安卓 - XML布局 vs 编程实现

5

我正在尝试使用继承自View的自定义类和一些按钮来使用相对布局。这是我最终想要的效果:

http://imgur.com/B5MtdJ7

抱歉我发了一个链接而不是图片,显然我还不够酷

目前这个高度是以dp为单位硬编码的(见下面的XML),这里有两个问题:

  • 只有在我的Nexus 7屏幕上看起来可以接受,在其他设备上不行
  • 两个自定义视图的onDraw方法仍然提供了一个具有与设备分辨率完全匹配的高度和宽度的画布

如果我尝试将layout_height设置为wrap_content,则每个自定义视图都会尝试占用整个屏幕,这似乎符合第2点,但显然不是我想要的。

我想要实现的是一个相对布局,其中包含两个自定义视图,看起来与图片完全相同,但会根据其所在屏幕的尺寸进行缩放,并且自定义视图的画布实际上知道它所占屏幕的大小。我该如何做到这一点?

编辑:之所以这是“vs程序化”,是因为我认为重写on measure可能不是一个坏主意,但我不知道它会如何与XML交互。我宁愿在一个地方定义布局。

我的XML如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TrajectoryView" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

    <com.sportsim.virtualcricket.view.SideProfileView
        android:id="@+id/side_profile_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.sportsim.virtualcricket.view.SideProfileView>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

    <com.sportsim.virtualcricket.view.BirdsEyeView
        android:id="@+id/birds_eye_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.25"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:text="Button" />
</LinearLayout>

1个回答

1
使用LinearLayout和weights来实现这个功能非常容易。我在下面给出了一个示例,但目前无法测试。不过它应该能提供一些方向。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TrajectoryView" >

<com.sportsim.virtualcricket.view.SideProfileView
    android:id="@+id/side_profile_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"/>

<com.sportsim.virtualcricket.view.BirdsEyeView
    android:id="@+id/birds_eye_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.25" />

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.25"
    android:orientation="horizontal">

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:text="Button" />

</LinearLayout>
</LinearLayout>

谢谢,我回家后会试试看。这个方法可以保留按钮的状态(即每个按钮占屏幕宽度的一半,并且彼此相邻吗)?虽然这只是一个测试活动,但我希望能从中学到尽可能多的知识。 - Imran
好的,我会修改它,使按钮并排放置。现在应该没问题了。再次说明,我还没有测试过它,通常嵌套权重对性能不利。您可以使用RelativeLayout替换用于设置按钮位置的LinearLayout。 - confused_at_times
啊,不错啊,RelativeLayout听起来很不错 - 谢谢啊 :D - Imran
测试过了。应该会给你想要的结果。祝你的项目好运! - confused_at_times
我已经尝试了一些东西,但它仍然没有按照设计的那样工作,我将编辑问题以显示我的新问题:P - Imran
你不需要所有这些子线性布局.. 只需要一个主LinearLayout和一个用于按钮的子LinearLayout。 - confused_at_times

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接