gpt4 book ai didi

android - 在 Android 应用程序中设置文本大小

转载 作者:行者123 更新时间:2023-11-30 03:39:50 25 4
gpt4 key购买 nike

我正在使用 Eclipse 编写一个 android 应用程序并使用最新的 API 进行编译。

我正在设置多个 TextView 控件的 TextSize。一些控件是在 XML 中定义的,而一些 TextView 控件是在代码中动态创建的。当我查看应用程序中的 TextView 控件时,文本大小不同。我使用相同的尺寸设置文本大小,所以我不明白为什么会出现不同的结果。

这是我定义的维度:

<!--  Default Font Size -->
<dimen name="font_size">12sp</dimen>

这是我在 XML 中设置其中一个 TextView 控件的地方:

<TextView
android:id="@+id/lblSection1Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/device_info_title"
android:textColor="@color/FontColor"
android:textSize="@dimen/font_size" />

这里是我在代码中设置文本大小的地方:

m_fTextSize = getResources().getDimension( R.dimen.font_size );
txtLabel = new TextView(this.m_xContext);
xLayoutParams = new TableRow.LayoutParams( 0, LayoutParams.WRAP_CONTENT, 0.35f );
xLayoutParams.setMargins((int)m_fMarginLeft, (int)m_fMarginTop, (int)m_fMarginRight, (int)m_fMarginBottom);
txtLabel.setLayoutParams(xLayoutParams);
txtLabel.setTextColor(this.m_iTextColor);
txtLabel.setTextSize(this.m_fTextSize);

如果您查看屏幕截图,您会发现动态创建的 TextView 控件小于屏幕上在 XML 中定义的其余 TextView。

这是屏幕截图的链接: http://emildiego.com/systemprofiler/wp-content/uploads/sites/2/2013/04/screen-shot-text-copy.png

我知道调用 getDimension 会根据屏幕密度调整文本大小,但在 XML 中设置 TextSize 不会做同样的事情吗?

在此先感谢您的帮助。

最佳答案

您的答案可以在这里找到:http://developer.android.com/reference/android/content/res/Resources.html#getDimension(int)

当 Android 返回 getDimension() 的值时,它已经在调整屏幕密度。尝试改用 getDimensionPixelSize()

关于android - 在 Android 应用程序中设置文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15948500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com