4

OK、スマートフォン ビューに 3 つのタブ、タブレット ランドスケープに 3 つの列を表示するユニバーサル アプリを作成しようとしています。そのため、Linear Layout を使用していくつかの異なる XML レイアウトを作成しました。

ただし、フラグメントを使用すると、問題が発生しました。そのフラグメントで選択が行われたときに、一番左の列を削除したいです。私はこれができると信じていましたが、XML で宣言されたフラグメントを削除できないことが判明しました。そこで、XML で中央と右の列を作成しようとしています。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/classification_fragment"
        android:name="uk.colessoft.android.hilllist.fragments.MenuClassificationFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/list_classifications" >
    </fragment>

    <fragment
        android:id="@+id/map"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

次に、を使用して最初の列を追加します

FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        MenuCountryFragment countryFragment=new MenuCountryFragment();
        fragmentTransaction.add(R.id.menu_layout, countryFragment, "country_menu").commit();

しかし、私には2つの問題があります:

a) 上記のコードは、フラグメントを線形レイアウトの右側に配置するだけで、位置を指定できません。左側にある必要があります。

b) XML により、画面のサイズ/向きに基づいて、フラグメントのさまざまな重みを自由に定義できるようになりました。Fragments でプログラムによってレイアウト パラメータを設定することは、後退したように思えます。これからレイアウトパラメータを読み取り、onCreateViewに割り当てるプレースホルダビューをxmlに配置することで、これを回避しましたが、ハックです。

xml の最初の要素の子としてフラグメントを追加することで、フラグメントを正しい場所に配置できることはわかっていますが、これを行ってフラグメントを削除すると、空白が残ります。

4

1 に答える 1

2

(XML で) 追加する新しいフラグメントのプレースホルダーを次のように作成します...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />


    <fragment
        android:id="@+id/classification_fragment"
        android:name="uk.colessoft.android.hilllist.fragments.MenuClassificationFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/list_classifications" >
    </fragment>

    <fragment
        android:id="@+id/map"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

次に、実行時に新しいフラグメントを追加する代わりに、空のコンテナーの内容を置き換えます...

FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MenuCountryFragment countryFragment=new MenuCountryFragment();
fragmentTransaction.replace(R.id.realtabcontent, newFrag).commit();
fragmentManager.executePendingTransactions();

お役に立てれば。

于 2013-02-20T23:50:39.720 に答える