为什么dimen资源不一致,如何取到正确的值?
Why is the dimen resource inconsistent and how to get the correct value?
我正在编写一个 Android 应用程序。我基本上创建了以下 dimen
资源:
<dimen name="button_text_size">11pt</dimen>
我在 xml 和 Java 代码中都使用它来创建 Button
和 TextView
,文本大小为 11pt。这是我的做法:
XML
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:textSize="@dimen/button_text_size"
android:text="secret!"
android:layout_margin="@dimen/button_margin"
android:background="secret!"
android:layout_weight="1"
android:onClick="secret!"/>
Java代码
TextView text = new TextView (this);
text.setLayoutParams (new LinearLayout.LayoutParams (0, ViewGroup.LayoutParams.WRAP_CONTENT, 3));
text.setText (secret);
text.setTextSize (getResources().getDimension (R.dimen.button_text_size));
理论上,两个视图的文字大小应该一样。但是文本视图的文本大小似乎更大!我不知道为什么会这样。我想这是因为设备存在一些问题,但实际发生了什么?如何获得正确的值?
使用dp or dip
代替pt
<dimen name="button_text_size">11dp</dimen>
dp/dip: Density-independent Pixels - An abstract unit that is based on the
physical density of the screen. These units are relative to a 160 dpi
(dots per inch) screen, on which 1dp is roughly equal to 1px. When
running on a higher density screen, the number of pixels used to draw
1dp is scaled up by a factor appropriate for the screen's dpi.
Likewise, when on a lower density screen, the number of pixels used
for 1dp is scaled down.
另一方面
pt:Points - 1/72 of an inch based on the physical size of the screen.
px: Pixels - Corresponds to actual pixels on the screen. This unit of
measure is not recommended because the actual representation can vary
across devices;
因此 pt 和 px 将因屏幕而异 而 dp**/dip 将根据屏幕进行管理。**..如果您希望在不同屏幕上保持一致的尺寸争取 dp/dip
有关 DIMENSIONS 的更多信息 refer
因此在 XML 中将 11pt 更改为 11dp 这将有所帮助
但动态地,它被认为是 px 的浮动值...所以请寻求 pavan 提供的解决方案,如
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.button_text_size));
选择以下值之一。
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,getResources().getDimension(R.dimen.button_text_size));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,getResources().getDimension(R.dimen.button_text_size));
试试这个
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.button_text_size));
最好根据 android 文档使用 sp( "scaled pixel") 作为文本大小
http://developer.android.com/intl/es/reference/android/widget/Button.html
我正在编写一个 Android 应用程序。我基本上创建了以下 dimen
资源:
<dimen name="button_text_size">11pt</dimen>
我在 xml 和 Java 代码中都使用它来创建 Button
和 TextView
,文本大小为 11pt。这是我的做法:
XML
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:textSize="@dimen/button_text_size"
android:text="secret!"
android:layout_margin="@dimen/button_margin"
android:background="secret!"
android:layout_weight="1"
android:onClick="secret!"/>
Java代码
TextView text = new TextView (this);
text.setLayoutParams (new LinearLayout.LayoutParams (0, ViewGroup.LayoutParams.WRAP_CONTENT, 3));
text.setText (secret);
text.setTextSize (getResources().getDimension (R.dimen.button_text_size));
理论上,两个视图的文字大小应该一样。但是文本视图的文本大小似乎更大!我不知道为什么会这样。我想这是因为设备存在一些问题,但实际发生了什么?如何获得正确的值?
使用dp or dip
代替pt
<dimen name="button_text_size">11dp</dimen>
dp/dip: Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down.
另一方面
pt:Points - 1/72 of an inch based on the physical size of the screen.
px: Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices;
因此 pt 和 px 将因屏幕而异 而 dp**/dip 将根据屏幕进行管理。**..如果您希望在不同屏幕上保持一致的尺寸争取 dp/dip
有关 DIMENSIONS 的更多信息 refer
因此在 XML 中将 11pt 更改为 11dp 这将有所帮助
但动态地,它被认为是 px 的浮动值...所以请寻求 pavan 提供的解决方案,如
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.button_text_size));
选择以下值之一。
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,getResources().getDimension(R.dimen.button_text_size));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,getResources().getDimension(R.dimen.button_text_size));
试试这个
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.button_text_size));
最好根据 android 文档使用 sp( "scaled pixel") 作为文本大小 http://developer.android.com/intl/es/reference/android/widget/Button.html