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