结合layout_weight和maxHeight使用的方法?

3
我有以下“按钮工具栏”布局设置:
<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%高度与恒定最大高度布局限制吗?

2
很遗憾,RelativeLayout 的 API 没有包括 maxHeight 属性(ViewViewGroup 也没有)。这就是为什么我认为它在你的代码中没有被尊重的原因。 - Flo
1个回答

1

如果您想要创建自己的布局计算,那么子类化 LinearLayout 可能是最简单且最干净的解决方案。

只需重写 onLayout()onMeasure() 即可。


谢谢,这对我来说已经足够了。但为了完全回答这个问题,放一些代码在这里会很好。我可能很快就会这样做... - Blackhex

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