2013-06-11 33 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