安卓得到片段宽度 [英] Android get Fragment width

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

问题描述

我有3个片段布局:

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

使用此线I得到的宽度从三个片段,不仅其中一个包含自定义的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开始,则prevent它从改变片段的宽度。然后你可以使用一个视图树观测获得的任何片段你感兴趣的宽度(通过查看其根视图)发生在布局之后。最后,你可以设置你的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).

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

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