Android 自动调整文本视图和布局的大小

Android 自动调整文本视图和布局的大小,android,Android,我有一个自定义对话框,其中显示了一些结果。结果文本可以有不同的大小,当其在对话框中很小时,有许多可用空间。如何为结果自动调整文本视图大小和布局自动调整大小 <LinearLayout android:layout_width="fill_parent" android:layout_height="300dp" android:background="@drawable/dialog_bg" android:orientati

我有一个自定义对话框,其中显示了一些结果。结果文本可以有不同的大小,当其在对话框中很小时,有许多可用空间。如何为结果自动调整文本视图大小和布局自动调整大小

<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>


将线性布局的高度和宽度更改为
wrap\u content
,同时使用填充或边距,在文本视图中使用
wrap\u content

不要硬编码任何值

只要用下面的自定义类替换任何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结尾)