有没有办法使用布局宽度和高度的权重

时间:2014-04-02 22:19:18

标签: java android android-layout

我有一个布局,我的应用程序底部有四个按钮,使用布局权重均匀分布。

我还想使用相同的加权来增加按钮的高度。

到目前为止,我所能看到的是使用水平线性布局或使用垂直线性布局来计算宽度。

宽度很高,只是高度。

如果无法同时使用重量,我如何设置高度,使其在每个屏幕尺寸上都是相对的。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:weightSum="1.0" 
    android:gravity="bottom">

    <ImageButton
        android:id="@+id/Button1"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_weight="0.25"
        android:src="@drawable/b1"
        android:text="Left" />

    <ImageButton
        android:id="@+id/Button2"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_weight="0.25"
        android:src="@drawable/b2"
        android:text="RotL" />

    <ImageButton
        android:id="@+id/Button3"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_weight="0.25"
        android:src="@drawable/b3"
        android:text="RotR" />

    <ImageButton
        android:id="@+id/Button4"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_weight="0.25"
        android:src="@drawable/b4"
        android:text="Right" />

  </LinearLayout>

我不想硬编码按钮的高度

2 个答案:

答案 0 :(得分:4)

  1. Button s的高度设为fill_parent
  2. 水平 LinearLayout置于垂直 LinearLayout
  3. 设置水平 LinearLayout的重量&lt; - 现在这是一个会影响Button s'高度的垂直重量
  4. 按钮应正确调整大小

答案 1 :(得分:1)

只是为了添加,您必须考虑布局优化,如(http://developer.android.com/training/improving-layouts/optimizing-layout.html)所述,您不应该使用嵌套权重,因为它们非常昂贵,因为每个孩子需要测量两次。

在我的情况下,我可以尝试GridLayoutInstead或RelativeLayout来完成你想要的结果。