合并视图的layout_weight和maxWidth [英] Combine layout_weight and maxWidth for views
问题描述
我想让我的布局更加景观友好,一个常见的模式是添加一个 LinearLayout
与一些按钮(例如确定|取消),只是设置 layout_weight
的值,以使空间均匀分布。问题是,当您在横向模式下使用手机或(特别是)平板电脑时,最终会出现看起来很可笑的巨大按钮。我尝试在按钮和线性布局上设置 maxWidth
,但没有效果。什么是最简单的方法来实现呢?即,设置视图的最大水平尺寸,使得其不会增长到整个宽度。我尝试了一堆不同的东西,但没有工作。
I'm trying to make my layouts more landscape friendly, and a common pattern I have is to add a LinearLayout
with some buttons (e.g. OK | Cancel) and just set a value for layout_weight
so that the space is evenly distributed. The problem is that when you use a phone or (especially) a tablet in landscape mode, you end up with humongous buttons that look ridiculous. I tried setting maxWidth
on the buttons and on the linear layout but to no avail. What would be the easiest way to achieve this? Namely, setting a maximum horizontal size for the view so that it does not grow to the whole width. I tried a bunch of different things, but none worked.
这里是一个示例布局:
<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>
注意:我知道我可以创建一个新的布局文件,
Note: I know I can create a new layout file for landscape and do magic there. I want to keep the specialized layout files to a minimum, and I would hope this does not require them.
推荐答案
我不需要这些特殊的布局文件,在这里测试它,但我会使用RelativeLayout,如下:
I don't have Eclipse here to test it, but I would use a RelativeLayout instead, something like:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<View
android:id="@+id/centerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/centerView"
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_toRightOf="@id/centerView"
android:text="@string/button2"
/>
</RelativeLayout>
正如我所说,我现在无法测试,但你可以解决这个问题。
As I said, I can't test it right now, but you can work around this idea.
在旁注中,除非你的布局很简单,我通常为风景创建单独的布局。这是一个更多的工作,但你可以优化的视图更好的那种方式。特别是,如果你打算支持更大的屏幕。
On a side note, unless your layout is very simple, I usually create separate layouts for landscape. It's a little more work, but you can optimize the views much better that way. Specially, if you plan to support larger screens.
这篇关于合并视图的layout_weight和maxWidth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!