2013-06-11 35 views
1

我正在做一个应用程序的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”

回答

0

我相信得到的TextView的文本大小。

String textSize = homepageTitle.getTextSize(); 

对于这一点,你必须指定大小

style="android:textSize="18sp"" 

不符合。但是

android:textSize="18sp" 

将符合条件。

希望这个工程。

+0

对不起,在我原来的问题的困惑。我的意思是我用'android:textSize =“18sp”'替换了整个'style =“”'行,这样就没有关系了。它仍然给我1.0。编辑原始问题的清晰度 – levibostian

0

我敢肯定getTextSize()返回一个float:P

检查this

用途:

float textSize = homepageTitle.getTextSize(); 
+0

你说得对,这是我在这个问题上的一个错误。我的程序有这一行。我在上面的原始问题中添加了我的确切实现。 – levibostian