结合layout_weight和maxHeight?

时间:2012-02-09 20:18:13

标签: android layout

我有以下“按钮栏”布局设置:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  <RelativeLayout
    android:id="@+id/button_bar"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:maxHeight="86px"
    android:orientation="horizontal">
    <Button
      android:id="@+id/home_button"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_alignParentLeft="true"/>
    <Button
      android:id="@+id/level_button"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_toRightOf="@+id/home_button"/>
    <Button
      android:id="@+id/help_button"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_toRightOf="@+id/level_button"/>
    <Button
      android:id="@+id/sound_button"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_alignParentRight="true"/>
  </RelativeLayout>
  <View
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"/>
</LinearLayout>

我希望按钮栏(RelativeLayout)高度为屏幕高度的10%,所以我使用layout_weight =“0.1”+ layout_weight =“0.9”,但不超过86像素,所以我尝试使用maxHeight =“86px”,但这不起作用。

您是否有任何线索如何获得10%的高度以及恒定的最大高度布局限制?

1 个答案:

答案 0 :(得分:1)

这是子类LinearLayout的最简洁且可能是最干净的解决方案,并创建自己的布局计算。

只需覆盖onLayout()onMeasure()