13

レイアウトには 2 つのボタンがあり、大画面デバイス (タブレット) では、ばかげて見えないように幅を制限したいと考えています。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

Remove the android:layout_weight="1" line from both buttons.
Add android:minWidth="50dp" and android:layout_width="wrap_content"

EDIT:

You need to calculate the button sizes manually depending on the screen size. First you need to have a standard screen size set. For example, If the you develop the app on a screen width 400px, and the button is 100px wide, then the formula for maintaining the same button width to screen width ratio on all devices will be as follows

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

Use case:
If screen width = 480px, then button width should be 120px.

于 2011-08-26T14:50:37.653 に答える
3

これを解決する良い方法は、画面解像度の異なる組み合わせに対して複数のレイアウトを作成することです。最大限に伸ばしたら、必要に応じて固定値を持つ新しいレイアウトを作成します。http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFiltersの「レイアウト エイリアスの使用」に関するセクションを参照してください。

于 2012-12-30T03:54:05.840 に答える
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 に答える
0

layout_width を wrap_content と 0dp に設定してみてください。

于 2011-08-26T14:08:29.657 に答える