loe*_*chg 38 android android-custom-view android-layout
我在自定义视图的构造函数中调用以下内容:
private void style(Resources.Theme theme, AttributeSet attrs) {
TypedArray a = theme.obtainStyledAttributes(
attrs,
R.styleable.StackedTextView,
0, 0);
try {
DebugTool.assertTrue(holdr != null, "View holder has not been properly intialized.");
String line1 = a.getString(R.styleable.StackedTextView_line1);
setLine1(line1);
String line2 = a.getString(R.styleable.StackedTextView_line2);
setLine2(line2);
line1Size = a.getDimension(R.styleable.StackedTextView_line1_textSize, 20);
line2Size = a.getDimension(R.styleable.StackedTextView_line2_textSize, 20);
if (line1Size > 0) {
holdr.textLine1.setTextSize(line1Size);
}
if (line2Size > 0) {
holdr.textLine2.setTextSize(line2Size);
}
} finally {
a.recycle();
}
}
Run Code Online (Sandbox Code Playgroud)
它应该为2个文本字段设置文本和文本大小.
除了文本内容的字符串格式之外,我在attr.xml中还有以下内容(工作正常).
<attr name="line1_textSize" format="dimension" />
<attr name="line2_textSize" format="dimension" />
Run Code Online (Sandbox Code Playgroud)
当我使用此视图并通过xml使用维度设置文本大小时,
<com.me.app.view.component.StackedTextView
android:id="@+id/overview_total_reviews"
app:line1="40"
app:line2="Rating"
style="@style/OverviewStackedText"
app:line1_textSize="10sp"
app:line2_textSize="12sp"
/>
Run Code Online (Sandbox Code Playgroud)
该文本最终明显大于预期.我只分别设置10和12sp,文本大小更接近30sp.
谁能看到我做错了什么?我是否需要对DisplayMetrics执行某些操作以确保正确缩放内容?
编辑:添加一些说明
尺寸正在被提升.当我使用自定义属性(在xml中)设置不同的文本大小时,文本会发生变化.我也试过用getDimensionPixelSize
.
就像计算/维度检索错误一样.1sp(或dp)的变化会导致重大变化.
pla*_*her 58
请尝试以下方法:
line1Size = a.getDimensionPixelSize(R.styleable.StackedTextView_line1_textSize, 0);
line2Size = a.getDimensionPixelSize(R.styleable.StackedTextView_line2_textSize, 0);
if (line1Size > 0) {
holdr.textLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, line1Size);
}
if (line2Size > 0) {
holdr.textLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, line2Size);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8896 次 |
最近记录: |