我正在做一个应用程序的Robolectric测试。 (带ABS的Robolectric 2.1)。在我的片段,我有风格的TextView:getTextSize()总是返回1.0的片段TextView
<TextView android:id="@+id/homepage_title_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingTop="@dimen/loading_textview_padding_top"
android:text="@string/loading_homepage_content"
android:textColor="@color/gray"
style="@android:style/TextAppearance.Medium"/>
任何时候,我尝试:
TextView homepageTitle = (TextView) mFragment.getView().findViewById(R.id.homepage_title_textview);
float textSize = homepageTitle.getTextSize();
它总是返回1.0。我已经测试了TextView中已创建并可见:
assertThat(homepageTitle.getVisibility(), equalTo(View.VISIBLE));
我也改变了整个style="@android:style/TextAppearance.Medium"
线是android:textSize="18sp"
而不是看它是否会有所作为,事实并非如此。
如果有帮助,这是我的确切Robolectric测试:
int style = android.R.style.TextAppearance_Medium;
int[] attrWanted = {android.R.attr.textSize};
TypedArray styleValues = Robolectric.application.getApplicationContext().obtainStyledAttributes(style, attrWanted);
String wantedTextSize = styleValues.getString(0);
String actualTextSize = String.valueOf(myTextView.getTextSize());
wantedTextSize返回 “18SP”
actualTextSize返回 “1.0”
对不起,在我原来的问题的困惑。我的意思是我用'android:textSize =“18sp”'替换了整个'style =“”'行,这样就没有关系了。它仍然给我1.0。编辑原始问题的清晰度 – levibostian