13

我在布局上有两个按钮,在大屏幕设备(平板电脑)上,我想限制它们的宽度,以免它们看起来很荒谬。我希望使用 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>

为什么,以及如何解决这个(显然是疯狂的)限制?

4

4 回答 4

12

android:layout_weight="1"从两个按钮中 删除线。
添加android:minWidth="50dp"android:layout_width="wrap_content"

编辑:

您需要根据屏幕尺寸手动计算按钮尺寸。首先,您需要设置标准的屏幕尺寸。例如,如果您在屏幕宽度 400px 上开发应用程序,并且按钮为 100px 宽,那么button width to screen width在所有设备上保持相同比例的公式将如下所示

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 buttonWidth = 100 * (metrics.widthPixels/400); 

用例:
如果屏幕宽度 = 480px,则按钮宽度应为 120px。

于 2011-08-26T14:50:37.653 回答
3

解决这个问题的好方法是为不同的屏幕分辨率组合创建多个布局。当你达到最大拉伸时,在需要的地方创建一个具有固定值的新布局。请参阅http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters中有关“使用布局别名”的部分

于 2012-12-30T03:54:05.840 回答
0

尝试将 layout_width 设置为 wrap_content 与 0dp。

于 2011-08-26T14:08:29.657 回答
0

宽度/高度似乎总是要设置在一起,..这在我看来是有效的

        <Button
            android:text="Center"
            android:layout_width="100dp"
            android:layout_height="fill_parent"
            android:id="@+id/selectionCenterButton"
            android:minWidth="50dp"
            android:minHeight="50dp"
            android:maxWidth="100dp"
            android:maxHeight="50dp"
            android:layout_weight="1" />

按钮的父级设置为包裹内容,因此按比例缩小,但最大宽度为 400(4 个按钮)

于 2012-10-06T02:56:38.340 回答