我使用 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>
感谢您的宝贵时间。
最佳答案
我认为您使用的 layout_weight
很糟糕。看看this question以便更好地理解。
你必须考虑到layout_weight
只适用于LinearLayout并且只描述一种方向(LinearLayout orientation)
为了设置高度百分比,您必须为 LinearLayout 设置 layout_weight
。但是由于要计算views witdh和height,所以真的不推荐。
可以看PercentFrameLayout或 PercentRelativeLayout它可以帮助你。
关于android - 使用宽度 Android Studio 的布局权重时调整高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150416/