Android - XML布局与程序化

Imr*_*ran 5 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>
Run Code Online (Sandbox Code Playgroud)

con*_*mes 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>
Run Code Online (Sandbox Code Playgroud)