Android:如何更改 EditText 字段中的文本大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to change the textsize in an EditText field?
提问by mmo
I want to display a table with some fields being editable - see this example:
我想显示一些字段可编辑的表 - 请参阅此示例:
<?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>
Why is the textSize attribute ignored in EditText-fields? How can I make the text in those fields smaller, so that ALL cells have the same font size? Why does Android not honor that attribute, even if explicitly set?
为什么 EditText 字段中的 textSize 属性被忽略?如何使这些字段中的文本更小,以便所有单元格具有相同的字体大小?为什么 Android 不尊重该属性,即使明确设置?
Michael
迈克尔
回答by Than Zaw
I think you should use sp instead of dp.
我认为你应该使用 sp 而不是 dp。
android:textSize="20sp"
回答by Konstantin Burov
Try 'dip' instead of 'dp' for textSize property. I dunno why, but sometimes using 'dp' takes no effect, even though documentation says that those two are equal. So I always use 'dip'.
为 textSize 属性尝试使用 'dip' 而不是 'dp'。我不知道为什么,但有时使用 'dp' 不起作用,即使文档说这两个是相等的。所以我总是使用'dip'。
回答by samer
Try the following in your main_Activity.java
在 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);
}
});
回答by Evan Langlois
Stupid system won't let me downvote or comment.
愚蠢的系统不会让我投票或评论。
sp is also density-independent. The difference between sp and dp is that sp is scaled according to the user's font size preferences. This means that all fonts and user interface elements that need to scale with the size of the font, should be done in sp units. Only items that should not scale with the font size should be done in dp units.
sp 也与密度无关。sp 和 dp 的区别在于 sp 根据用户的字体大小偏好进行缩放。这意味着所有需要随字体大小缩放的字体和用户界面元素都应以 sp 为单位。只有不应随字体大小缩放的项目才应以 dp 为单位。
回答by Manikandan K
In your styles.xml
在你的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>