Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获得碎片宽度?_Android_Android Fragments_Fragment - Fatal编程技术网

Android 如何获得碎片宽度?

Android 如何获得碎片宽度?,android,android-fragments,fragment,Android,Android Fragments,Fragment,这是我的布局。我需要得到CategoryFragment中的宽度 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLay

这是我的布局。我需要得到CategoryFragment中的宽度

<LinearLayout 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/category_list"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:background="@color/white_background"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/pianodule_list"
            android:textSize="16sp" />

        <com.handmark.pulltorefresh.library.PullToRefreshListView
            android:id="@id/refreshable_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:descendantFocusability="beforeDescendants"
            android:divider="@null" />
    </LinearLayout>

    <View
        android:layout_width="0.1dp"
        android:layout_height="match_parent"
        android:background="@color/divider_line" />

    <fragment
        android:name="com.srefu.frag.CategoryFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="category" />
</LinearLayout>
我该怎么做才能得到碎片的宽度?任何帮助都将不胜感激。谢谢。)

调用getWidth()太早了。 尝试下面提到的代码。希望对你有帮助

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mScrollView.post(new Runnable() {
            public void run() {
                view.getHeight(); //height is ready
            }
        });
    }
});

太棒了,对我有用!我同意你的说法,我调用getWith()太早了。谢谢。:)当你达到高度时,别忘了移除GlobalYoutListener,否则可能会泄漏。如果您只需要一次,您应该在获得高度后立即使用RemoveOnGlobalYoutListener。@cjbrooks12感谢您的提醒。
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mScrollView.post(new Runnable() {
            public void run() {
                view.getHeight(); //height is ready
            }
        });
    }
});