【发布时间】:2015-10-28 10:59:27
【问题描述】:
当我通过布局 XML 和 Java 代码设置 TextView 的文本大小时,我得到不同的文本大小。我的布局如下所示。
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
android:textSize="18sp"/>
我尝试通过以下两种方式在 Java 中设置文本大小。
选项 1
TextView titleView = (TextView) view.findViewById(R.id.title);
titleView.setTextSize(18 * getResources().getDisplayMetrics().density);
选项 2
TextView titleView = (TextView) view.findViewById(R.id.title);
titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Java 代码设置的文本大小(选项 1 和 2)看起来比布局 XML 设置的文本大小大得多。我做错了什么?
【问题讨论】:
-
请不要在提交后的 5 分钟内通过投反对票来阻止人们提问。我花了一些时间搜索它,又花了一些时间写这个问题。我总是有可能找不到我正在寻找的关于 stackoverflow 的确切帖子。
标签: android android-layout textview android-xml