Android获取片段宽度 [英] Android get Fragment width

查看:22
本文介绍了Android获取片段宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个片段的布局:

I have a layout with 3 fragments:

<LinearLayout android:id="@+id/acciones"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

</LinearLayout>


<LinearLayout
android:id="@+id/fragment2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:id="@+id/f3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

</LinearLayout>

</LinearLayout>

在第一个片段中,我有一个 TableLayout,其中每一行都有一个自定义 TextView.我想知道片段的宽度,因为如果自定义的 TextView 比片段宽,我会设置必要的行数.

In the first fragment I have a TableLayout in which I have one custom TextView in each row. I want to know the width of the fragment because if the custom TextView is wider than the fragment, I'll set the number of lines necessary.

这是我在自定义 TextView 中所做的:

This is what I've done in my custom TextView:

@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mMaxWidth = (float) (getMeasuredWidth());
}

通过这一行,我得到了三个片段的宽度,而不仅仅是包含自定义 TextView 的片段.

With this line I got the width from the three Fragments, not only the one which contains the custom TextView.

谢谢.

推荐答案

你应该可以将 TextView 的宽度设置为 fill_parent,在这种情况下它会为你做包装.您不应该将布局的宽度设置为 match_parent,因为当您使用布局权重时它效率低下.

You should be able to set the width of the TextView to be fill_parent, in which case it will do the wrapping for you. You should not set the widths of your layouts to be match_parent since it is inefficient when you're using layout weights.

由于 android 的布局系统在视图大小方面有时很神秘,如果将 TextView 宽度设置为 fill_parent 实际上会使其占据整个屏幕(正如您的问题所暗示的那样),请执行以下操作:

Since android's layout system is occasionally mysterious with regards to view sizes, if setting the TextView width to be fill_parent actually makes it take up the whole screen (as your question appears to be implying) do the following:

默认情况下将您的 TextView 宽度设置为 0.在您的活动的 onCreate 中,设置内容视图后:

Set your TextView width to 0 by default. In onCreate of your activity, after setting the content view:

findViewById(R.id.acciones).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        final int fragmentWidth = findViewById(R.id.releventFragmentId).getWidth();
        if (fragmentWidth != 0){
            findViewById(R.id.yourTextViewId).getLayoutParams().width = fragmentWidth;
        }
    }
});

通过最初将 TextView 的宽度设置为 0,可以防止它改变片段的宽度.然后,您可以使用视图树观察器在布局发生后获取您感兴趣的任何片段的宽度(通过查看其根视图).最后,您可以将您的 TextView 设置为确切的宽度,然后它会自动为您进行包装.

By setting the TextView's width to 0 initially, you prevent it from changing the widths of the fragments. Then you can use a view tree observer to get the width of whatever fragment you're interested in (by looking at its root view) after layout has occurred. Finally you can set your TextView to be that exact width, which in turn will do the wrapping for you automatically.

请注意,onGlobalLayout 可以多次调用,并且会在所有视图完全布局之前定期调用,因此需要检查 != 0.您可能还需要进行某种检查以确保您只设置一次文本视图的宽度,否则您可能会进入无限布局循环(不是世界末日,但对性能不利).

Note that onGlobalLayout can be called multiple times and is regularly called before all of the views have been completely laid out, hence the != 0 check. You will also probably want to do some kind of check to make sure that you only set the width of the text view once, or otherwise you can get into an infinite layout loop (not the end of the world, but not good for performance).

这篇关于Android获取片段宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆