使用宽度Android Studio的布局权重时调整高度

时间:2016-07-01 16:48:19

标签: android android-layout android-studio

我使用Layout_Weight将按钮的宽度设置为屏幕宽度的40%。效果很好!但是现在我无法在高度上做很多事情。

理想情况下,我也希望设置高度的百分比,但据我所知,我只能将layout_width百分比设置为一维。

要调整按钮的高度,使它们看起来不像平板电脑上的小条,我尝试使用填充,尝试使用\ n。切换到平板电脑时,带有两行文字的按钮与单行文字不相等,但在手机上相同。

将宽度调整为动态的最佳方式是什么?像宽度一样?

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="This takes up two lines"
                android:id="@+id/btnDoubleLineExample"
                android:layout_weight="40"
                android:paddingTop="80dp"
                android:paddingBottom="80dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textSize="20sp"
                />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="One Line\n"
                android:id="@+id/btnOneLine"
                android:layout_weight="40"
                android:paddingTop="80dp"
                android:paddingBottom="80dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textSize="20sp"/>
        </LinearLayout>

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

我认为你使用得很糟before_action。请查看this question以获得更好的理解。

您必须考虑layout_weight仅适用于LinearLayout并仅描述一个方向(LinearLayout方向)

要设置高度百分比,您必须将layout_weight设置为LinearLayout。但由于计算了视图和高度,所以不建议使用它。

您可以查看PercentFrameLayoutPercentRelativeLayout它可以为您提供帮助。