我的布局上有两个按钮,在大屏幕设备(平板电脑)上我想限制它们的宽度,这样它们看起来就不会很荒谬。我希望使用 maxWidth 属性,但它显然在我的场景中没有任何作用。这是布局定义 - 按钮使用布局的整个宽度,忽略 maxWidth 中的任何值。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 2"
/>
</LinearLayout>
为什么以及如何解决这个(显然是疯狂的)限制?
去除android:layout_weight="1"
来自两个按钮的线。
Add android:minWidth="50dp"
and android:layout_width="wrap_content"
EDIT:
您需要根据屏幕尺寸手动计算按钮尺寸。首先,您需要设置标准屏幕尺寸。例如,如果您在屏幕宽度 400px 上开发应用程序,按钮宽度为 100px,则保持相同的公式button width to screen width
所有设备上的比率如下
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
buttonWidth = 100 * (metrics.widthPixels/400);
使用案例:
如果屏幕宽度 = 480px,则按钮宽度应为 120px。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)