自动调整文本视图和布局

问题描述:

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

<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_content的,也可以使用填充或余量,在文本视图使用wrap_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结尾)。