【问题标题】:Android: How to change the textsize in an EditText field?Android:如何更改 EditText 字段中的文本大小?
【发布时间】:2011-04-11 09:47:37
【问题描述】:

我想显示一个包含可编辑字段的表格 - 请参阅此示例:

<?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-fields 中忽略 textSize 属性?如何使这些字段中的文本更小,以便所有单元格具有相同的字体大小?为什么即使明确设置,Android 也不尊重该属性?

迈克尔

【问题讨论】:

  • 我可能弄错了,但我相信你应该为 android:textSize 元素使用“14px”而不是“14dp”
  • @mtmurdock,单位可以指定为 px(像素)、dp(与密度无关的像素)、sp(基于首选字体大小的缩放像素)、in(英寸)和 mm(毫米) . (来源:developer.android.com/reference/android/widget/…
  • "px" 应不惜一切代价避免使用,以便与各种 DPI 设备兼容。

标签: android android-widget


【解决方案1】:

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

android:textSize="20sp"

【讨论】:

  • 错误,dp i 固定单位,sp 受用户设置影响 - 在可访问性下(适用于有视力问题的人)
【解决方案2】:

为 textSize 属性尝试使用“dip”而不是“dp”。我不知道为什么,但有时使用 'dp' 不起作用,即使文档说这两者是相等的。所以我总是用'dip'。

【讨论】:

  • TEXT SIZE 怎么用“dp”或“dip”来衡量,据我认为应该用“sp”指定
【解决方案3】:

在您的 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); 
    }
});

【讨论】:

    【解决方案4】:

    愚蠢的系统不会让我投票或评论。

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

    【讨论】:

      【解决方案5】:

      在你的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>
      

      【讨论】:

        猜你喜欢
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2015-07-28
        • 2010-10-08
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多