1

我正在对应用程序进行 Robolectric 测试。(带 ABS 的 Robolectric 2.1)。在我的片段中,我有一个带有样式的 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());  

WantTextSize 返回“18sp”
actualTextSize 返回“1.0”

4

2 回答 2

0

I am pretty sure getTextSize() returns a float :P

Check this

Use:

float textSize = homepageTitle.getTextSize();
于 2013-06-11T23:25:12.047 回答
0

我相信可以获得textview的文本大小。

String textSize = homepageTitle.getTextSize(); 

为此,您必须指定大小

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

不符合条件。但

android:textSize="18sp"

将有资格。

希望这有效。

于 2013-06-11T23:21:32.723 回答