Android布局:测量视图,以便选择正确的TextView字体大小



我目前正在开发一个具有权重组件的Activity。这些组件包含文本和图标。据我所知,android不提供根据它的父视图缩放文本的功能。因此,我需要测量这些视图,然后手动将我的自定义字体应用于TextViews并设置适当的字体大小。

我目前正在做下面的方式(这实际上是有效的。但我经常得到09-30 17:55:14.844:ERROR/ViewRoot(12893): OutOfResourcesException锁定表面)错误,我相信这可能与我的布局方式有关。

这是布局中的一行(有几行)

<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));
    }

这是我想做的事情的正确方法吗?

谢谢你的建议。

欢呼

我发现了导致OutOfResourcesExceptions的问题。在onLayout中,我为TextView设置了文本。这将导致视图重新布局,从而导致无限循环。

我仍然有兴趣知道如果使用addOnGlobalLayoutListener和onLayout是唯一的方法来缩放TextViews相对于他们的动态大小的父视图。

最新更新