Android中TextView的TextAppearance属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24237616/
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
TextAppearance property of TextView in Android
提问by N Sharma
I am working on android application where I am using TextView
to display the text on screens.
我正在开发TextView
用于在屏幕上显示文本的android 应用程序。
I am using this property for the TextView
to set the size of the text android:textAppearance="?android:attr/textAppearanceMedium"
.
我正在使用此属性TextView
来设置文本的大小android:textAppearance="?android:attr/textAppearanceMedium"
。
Do I need to set the size of that text also or it does automatically manage by Android OS ?
我是否还需要设置该文本的大小,还是由 Android 操作系统自动管理?
Thanks in advance.
提前致谢。
采纳答案by Sylas Seabrook
In Android text elements there is a default which is applied first (similar to webpages). If you wish to override those and define your own, then you must explicitly set them. android:textAppearance
is one such override, but there are other attributes you can override individually (as opposed to as an aggregate)... you'll want to see the documentation for that.
在 Android 文本元素中,有一个首先应用的默认值(类似于网页)。如果您希望覆盖这些并定义您自己的,那么您必须明确设置它们。android:textAppearance
是这样的一种覆盖,但是您可以单独覆盖其他属性(而不是聚合)……您需要查看相关文档。
回答by Shubham Srivastava
Yes, the resizing is done automatically in the background. While using attributes like android:textAppearance
it directly uses the predefined specific sp values for the textSize as-
是的,调整大小是在后台自动完成的。使用类似的属性时android:textAppearance
,直接使用 textSize 的预定义特定 sp 值作为-
android:textAppearance="?android:textAppearanceSmall"
=> 14sp
android:textAppearance="?android:textAppearanceSmall"
=> 14sp
android:textAppearance="?android:textAppearanceMedium"
=> 18sp
android:textAppearance="?android:textAppearanceMedium"
=> 18sp
android:textAppearance="?android:textAppearanceLarge"
=> 22sp
android:textAppearance="?android:textAppearanceLarge"
=> 22sp
Thus it adjusts the values automatically based on the screen densityand the user's preferencein such a way that it seems equal on all the devices say for example formula for dp values: px = dp * (dpi/160), the similar case is for the sp values used only for fonts, it utilizes the same concept only difference being it may vary due to user's fontSize preference as set in settings.
因此,它会根据屏幕密度和用户的偏好自动调整值,使其在所有设备上看起来都相等,例如 dp 值的公式:px = dp * (dpi/160),类似的情况是sp 值仅用于字体,它使用相同的概念,唯一的区别是它可能因设置中设置的用户 fontSize 首选项而异。
I haven't used that :attr/
type as it may or may not be used and serves the same purpose because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type.
我没有使用该:attr/
类型,因为它可能会或可能不会被使用并且具有相同的目的,因为系统资源工具知道在此上下文中需要属性资源,您不需要显式声明类型。
Following are the links to external resources:
以下是外部资源的链接:
textAppearance equivalent sp size, "Android Developers, Google + article"
textAppearance 等效 sp 大小,《Android 开发者,Google+ 文章》
回答by Rod_Algonquin
Do I need to set the size of that text also or it does automatically manage by Android OS ?
我是否还需要设置该文本的大小,还是由 Android 操作系统自动管理?
Nope, Using android:textAppearance="?android:attr/textAppearanceMedium
will make all of the text in medium size on different devices. So that means your app text on different devices will have the same size and weight.
不,使用android:textAppearance="?android:attr/textAppearanceMedium
将使所有文本在不同设备上显示为中等大小。这意味着您在不同设备上的应用文本将具有相同的大小和重量。
Setting the size of each text would require calculation to be able to achieve same size and weight.
设置每个文本的大小需要计算才能达到相同的大小和重量。