50

膨大なコード ダンプで申し訳ありませんが、本当に迷っています。

MyActivity.java onCreate :

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();

PlacesFragment.java onCreateView :

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;

:mRootViewViewGroupグローバルです。問題はないと思います。PlacesFragmentですListFragment

レイアウト:

activity_singlepane_empty.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00f">
    <include layout="@layout/actionbar"/>

    <!-- FRAGMENTS COME HERE! See match_parent above -->

</LinearLayout>

list_content.xml :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listContainer"
        android:background="#990"
        >
    
        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />
        
        <TextView android:id="@id/android:empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" 
                android:text="@string/no_data_suggest_connection"/>

</FrameLayout>

問題:ご覧のとおり、予想される動作は、空TextViewの上部が画面の中央に表示されることです。EclipseのデザインプレビューではOKです。root_viewフラグメントとして追加された場合にのみ、FrameLayout画面全体を埋めません。

root_containerは青色で、FrameLayout黄色がかっています。デバッグ目的については以下を参照してください。黄色のペインが画面全体に表示されるべきではありませんか?!?!?!?

ここに画像の説明を入力

4

6 に答える 6

76

私も同じ問題を抱えていましたが、ここで行ったように、フラグメントのonCreateViewのレイアウトをnullで膨らませると発生すると思います。

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);

代わりに、これを行う必要があります。

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);

コンテナはビューグループです。少なくとも、それで問題は解決しました。

于 2012-03-12T14:26:38.790 に答える
6

何らかの理由で、FrameLayout は XML からレイアウトを取得しません。

コードでも設定する必要があります (フラグメントの onCreateView):

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
FrameLayout fl = (FrameLayout) mRootView.findViewById(R.id.listContainer);
fl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return mRootView;
于 2011-10-28T22:54:18.277 に答える
5
<android.support.v4.widget.NestedScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"
    >

    <include layout="@layout/screen_main_fragment" />

</android.support.v4.widget.NestedScrollView>

機能させるには、layout_height="wrap_content" を "match_parent" に変更する必要がありました。

于 2016-05-06T14:09:24.810 に答える
0

この行を親レイアウトに追加すると、その問題が解決します。

android:fillViewport="true"

于 2021-05-11T11:37:55.450 に答える