【发布时间】:2015-10-17 15:37:45
【问题描述】:
我正在编写一个 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));
理论上,两个视图的文本应该是相同大小的。但是文本视图的文本大小似乎更大!我不知道为什么会这样。我想这是因为单位的一些问题,但实际发生了什么?如何获得正确的值?
【问题讨论】:
标签: java android android-xml text-size