I'm calling the following in the constructor of my custom view:
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();
}
}
It's supposed to set text and text size for 2 textfields.
I have the following in my attr.xml in addition to the string formats for the text content (which works fine).
<attr name="line1_textSize" format="dimension" />
<attr name="line2_textSize" format="dimension" />
When I use this view and set the text size using a dimension via 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"
/>
the text ends up being significantly larger than expected. I'm only setting 10 and 12sp respectively, and the text sizes are closer to like 30sp.
Can anyone see what I'm doing wrong? Do I need to do something with DisplayMetrics to make sure things are scaled properly?
Edit: Adding some clarification
The dimension IS getting picked up. The text does change when I set the different text sizes using my custom attribute (in xml). I've also tried using getDimensionPixelSize
.
It's as if the calculation/dimension retrieval is wrong. 1sp (or dp) change results in a significant change.