我正在努力让我的布局更加横向友好,我的一个常见模式是添加一个 LinearLayout
使用一些按钮(例如确定 | 取消)并为 layout_weight
设置一个值使空间分布均匀。问题在于,当您在横向模式下使用手机或(尤其是)平板电脑时,您最终会看到看起来很可笑的巨大按钮。我尝试设置 maxWidth
在按钮和线性布局上,但无济于事。实现这一目标的最简单方法是什么?即,为 View 设置最大水平尺寸,使其不会增长到整个宽度。我尝试了很多不同的东西,但没有一个奏效。
这是一个示例布局:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="6dp"
android:paddingBottom="6dp">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:textStyle="bold"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button2" />
</LinearLayout>
注意:我知道我可以为景观创建一个新的布局文件并在那里施展魔法。我希望将专门的布局文件保持在最低限度,我希望这不需要它们。
最佳答案
在对相对布局进行了一番折腾之后,我偶然发现了 this answer ,这是我最终使用的。
关于android - 合并 View 的 layout_weight 和 maxWidth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6284911/