【问题标题】:Combining layout_weight and maxHeight?结合layout_weight和maxHeight?
【发布时间】:2012-03-02 08:26:19
【问题描述】:

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

<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% 的高度和恒定的最大高度布局限制吗?

【问题讨论】:

  • 不幸的是,RelativeLayout 的 API 不包含属性 maxHeightViewViewGroup 也不包含)。这就是为什么它在你的代码中没有得到尊重,我认为。

标签: android layout


【解决方案1】:

LinearLayout 进行子类化并创建自己的布局计算相当简单,并且可能是最简洁的解决方案。

只需覆盖 onLayout()onMeasure()

【讨论】:

  • 谢谢,这对我来说已经足够回答了,但是考虑到这个问题已经完全回答了,在这里放一些代码会很好。我可能很快就会这样做......
猜你喜欢
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多