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

发布于 2024-12-07 04:31:42 字数 3308 浏览 1 评论 0原文

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

我目前正在按以下方式执行此操作(这实际上有效。但我经常收到 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>

这是我进行测量的方式:

in onCreate:

LinearLayout mLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.activity_main, null);
    mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
    this.setContentView(mLayout);

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

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

非常感谢您的建议。

干杯

I am currently working on an Activity that features components with weight. Those components contain text and icons. As far as I know, android does not provide features to scale text according to it's parent view. As a consequence, I need to measure those views and then manually apply my custom font to the TextViews and set an appropriate font size.

I am currently doing it the following way (Which actually works. But I often get 09-30 17:55:14.844: ERROR/ViewRoot(12893): OutOfResourcesException locking surface
) errors and I believe this might be connected to the way I do my layout.

This is one row in the layout (there are several rows)

<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>

This is the way I do the measurements:

in onCreate:

LinearLayout mLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.activity_main, null);
    mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
    this.setContentView(mLayout);

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

Is this the correct way to do what I want to do?

Thanks so much for your advice.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2024-12-14 04:31:42

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

然而,我仍然有兴趣知道使用 addOnGlobalLayoutListener 和 onLayout 是否是相对于动态大小的父视图缩放 TextView 的唯一方法。

I found the issue that was causing the OutOfResourcesExceptions. In the onLayout, I set the text for a TextView. This causes the view to be laid out again, resulting in kind of an an infinite loop.

I'd however still be interested in knowing if using addOnGlobalLayoutListener and onLayout is the only way to scale TextViews in respect to their dynamically sized parent views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文