Android:如何在EditText字段中更改文本大小?

14

我想显示一个表格,其中一些字段可编辑-请参见此示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:paddingLeft="1dip"
 android:paddingRight="1dip"
 android:paddingBottom="1dip"
 android:background="#F00"
 >
 <ScrollView
  android:id="@+id/ScrollView01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scrollbars="vertical"
  >
  <HorizontalScrollView
   android:id="@+id/HorizontalScrollView01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:scrollbars="horizontal"
   >
   <TableLayout
    android:id="@+id/ItemPane"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#EEE"
   >
    <TableRow
     android:id="@+id/DataRow"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"    
     android:background="#888"
     android:layout_margin="0dp" 
     android:gravity="left"
     >
     <TextView
      android:id="@+id/NameField"
      android:text="Fieldname"
      android:background="#EEE"
      android:textSize="14dp"
      android:layout_margin="1dp" 
      android:layout_marginBottom="0dp" 
      android:paddingLeft="3dp"
      android:paddingRight="3dp"
     />
     <EditText
      android:id="@+id/DataField"
      android:text="some field"
      android:background="#EEE"
      android:textSize="14dp"
      android:layout_margin="1dp" 
      android:layout_marginBottom="0dp" 
      android:paddingLeft="3dp"
      android:paddingRight="3dp"
      android:singleLine="true"
     />
     <EditText
      android:id="@+id/DataField2"
      android:text="some longish field content..."
      android:background="#EEE"
      android:textSize="14dp"
      android:layout_margin="1dp" 
      android:layout_marginBottom="0dp" 
      android:paddingLeft="3dp"
      android:paddingRight="3dp"
      android:singleLine="true"
     />
    </TableRow>   
   </TableLayout>
  </HorizontalScrollView>
 </ScrollView>
</LinearLayout>
为什么EditText中的textSize属性被忽略了?我该如何使这些字段中的文本变小,以便所有单元格具有相同的字体大小?即使明确设置,为什么Android也不尊重该属性?

我可能错了,但我认为在android:textSize元素中应该使用“14px”而不是“14dp”。 - mtmurdock
@mtmurdock,可以使用px(像素)、dp(密度无关像素)、sp(根据首选字体大小缩放的比例像素)、in(英寸)和mm(毫米)来指定单位。 (来源:http://developer.android.com/reference/android/widget/TextView.html#attr_android:textSize) - Brian
应尽量避免使用"px",以确保与不同DPI设备的兼容性。 - warbi
5个回答

23

我认为你应该使用sp而不是dp。

android:textSize="20sp"

错误,dp我固定单位,sp受用户设置影响 - 在辅助功能下(为视力问题的人设计)。 - Wops

7
尝试使用"textSize"属性的"dip"而不是"dp"。我不知道为什么,但有时使用"dp"没有效果,尽管文档说这两个相等。所以我总是使用"dip"。

1
为什么文本大小要用“dp”或“dip”来衡量,我认为应该使用“sp”来指定。 - candy

2
请在您的main_Activity.java中尝试以下操作。
final Button btpls=(Button) findViewById(R.id.btpls);

btpls.setOnClickListener(new OnClickListener(){
    public void onClick(View t){
        float i=textfield.getTextSize();
       textfield.setTextSize(i+1) ; 
    }
});

final Button btmins=(Button) findViewById(R.id.btmins);

btmins.setOnClickListener(new OnClickListener(){
    public void onClick(View t){
        float n=textfield.getTextSize();
        textfield.setTextSize(n-1); 
    }
});

0

愚蠢的系统不让我点踩或评论。

sp也是密度无关的。 sp和dp之间的区别在于,sp根据用户的字体大小偏好进行缩放。这意味着所有需要随字体大小缩放的字体和用户界面元素都应该使用sp单位完成。只有那些不应随字体大小缩放的项目应该使用dp单位。


0
在你的styles.xml文件中。
<style name="HintTextSize" parent="TextAppearance.Design.Hint">
        <item name="android:textSize">16sp</item>
        <item name="android:paddingLeft">16sp</item>
    </style> 

<android.support.design.widget.TextInputLayout
    android:id="@+id/trailer_one_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    **app:hintTextAppearance="@style/TextLabel"
    android:layout_marginTop="10dp">

        <EditText
            android:id="@+id/edit_text_trailer_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="@string/trailer_one"
            android:inputType="text"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/DrivingDarkGrey"/>
    </android.support.design.widget.TextInputLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接