0

いくつかの結果を表示するカスタムダイアログがあります。結果のテキストはさまざまなサイズになる可能性があり、ダイアログの小さい場合は多くの空き領域があります。結果とレイアウトの自動サイズ変更のためにtextViewの自動サイズ変更を行うにはどうすればよいですか?

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:background="@drawable/dialog_bg"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.01"
            android:gravity="center"
            android:text="Scores"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="18sp"
            android:textStyle="italic"
            android:typeface="serif" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0.04"
            android:background="@null"
            android:cursorVisible="false"
            android:editable="false"
            android:ems="10"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="top|left"
            android:inputType="textMultiLine|textNoSuggestions|textFilter"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:textStyle="italic"
            android:typeface="serif" >

            <requestFocus />
        </EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:layout_margin="10dp" >

            <Button
                android:id="@+id/ok"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:background="@drawable/ok"
                android:onClick="okAction" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="0.99" >
            </LinearLayout>

            <Button
                android:id="@+id/share"
                android:layout_width="91dp"
                android:layout_height="wrap_content"
                android:background="@drawable/share"
                android:onClick="shareAction" />
        </LinearLayout>
    </LinearLayout>
4

2 に答える 2

1

TextView を以下のカスタム クラスに置き換えるだけです。

public class Text extends TextView {
    public Text(Context context, AttributeSet attrs) {
        super(context, attrs);
        float textSize=this.getTextSize();
        WindowManager wm=(WindowManager)(context.getSystemService(Context.WINDOW_SERVICE));
        Display display=wm.getDefaultDisplay();
        int width=display.getWidth();
        int height=display.getHeight();
        double diagonal=Math.sqrt(Math.pow(height,2)+Math.pow(width,2));
        double ratio=diagonal/1500.0;
        this.setTextSize((float)(ratio*textSize));
    }

}

1500.0 を、アプリをテスト/ビルドしているデバイスの対角線の画面サイズ (ピクセル単位) に置き換えます (ダブル形式である必要があるため、末尾に .0 を追加する必要があります)。

于 2015-03-08T06:05:43.893 に答える
0

線形レイアウトの高さと幅をに変更します。wrap_contentまた、パディングまたはマージンを使用します。テキストビューではを使用しますwrap_content
値をハードコーディングしないでください

于 2012-08-13T07:48:24.123 に答える