0
我目前正在开发一个以重量为特征的组件。这些组件包含文本和图标。据我所知,android不提供根据其父视图缩放文本的功能。因此,我需要测量这些视图,然后手动将自定义字体应用于TextView并设置适当的字体大小。我经常得到09-30 17:55:14.844:ERROR/ViewRoot(12893):OutOfResourcesException锁定表面 )错误,我相信这可能会被连接到我做我的布局的方式。Android布局:测量视图以选择正确的TextView字体大小
这是在布局一行(有几行)
<RelativeLayout
android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="0.35"
android:id="@+id/activity_main_row_1">
<View
android:layout_width="6dip"
android:layout_height="fill_parent"
android:background="@color/palette_green"
android:layout_alignParentLeft="true"
/>
<RelativeLayout
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingLeft="20dip">
<TextView
android:id="@+id/activity_main_row_1_title"
android:text="@string/title_add_expense"
android:textSize="10dip"
android:textColor="@color/palette_grey"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0px">
</TextView>
<TextView
android:id="@+id/activity_main_row_1_sub_title"
android:text="@string/subtitle_add_expense"
android:textSize="10dip"
android:textColor="@color/palette_dark_grey"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:layout_below="@+id/activity_main_row_1_title">
</TextView>
</RelativeLayout>
<ImageView
android:id="@+id/activity_main_row_1_bracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_bracket_right_grey"
android:paddingLeft="0dip"
android:paddingRight="0dip"
android:paddingTop="24dip"
android:paddingBottom="20dip"
android:layout_alignParentRight="true">
</ImageView>
</RelativeLayout>
这是我的方式做测量:
中的onCreate:
LinearLayout mLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.activity_main, null);
mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
this.setContentView(mLayout);
在onGlobalLayout:
@Override
public void onGlobalLayout() {
int mRow1Height = mRow1.getHeight();
<omitted>
if(mRow1Height>0) {
TextView row1Title = (TextView) findViewById(R.id.activity_main_row_1_title);
row1Title.setTypeface(mFontProvider.getTypeface());
row1Title.setTextSize((float) ((mRow1Height)*.3));
TextView row1SubTitle = (TextView) findViewById(R.id.activity_main_row_1_sub_title);
row1SubTitle.setTypeface(mFontProvider.getTypeface());
row1SubTitle.setTextSize((float) ((mRow1Height)*.2));
}
这是做我想做的事的正确方法吗?
非常感谢您的建议。
干杯