Android - XML布局与程序化

时间:2013-03-21 08:08:56

标签: android android-layout

我正在尝试使用相对布局和扩展视图的自定义类以及几个按钮。这就是我最终想要的样子:

http://imgur.com/B5MtdJ7

(原谅我张贴链接而非图片,显然我还不够酷)

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

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

如果我尝试将layout_height设置为wrap_content,则每个自定义视图都会尝试占用整个屏幕,这与子弹点2一致,但显然不是我想要的。

我想要实现的是具有两个自定义视图的相对布局,其完全如图所示,但会将其自身缩放到它所在的屏幕的尺寸上,并且自定义视图画布实际上知道它有多大它坐在屏幕上。我该怎么做?

编辑:这是“vs programmatic”的原因是我认为覆盖测量不会是一个糟糕的呼喊,但我不知道它将如何与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 个答案:

答案 0 :(得分:1)

使用LinearLayout和权重可以相当容易。我在下面给出了一个例子,但我现在无法测试它。它应该提供一些方向。

<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>
相关问题