Fragment トランザクションを使用して、アクティビティに 2 つのフラグメントを追加しています。ただし、アプリの起動時に最初の Fragment のみが表示されることがあります。コードは次のとおりです。
主な活動
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragOne firstButton = new FragOne();
FragmentTwo secButton = new FragmentTwo();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frag_container, firstButton);
transaction.add(R.id.frag_container, secButton);
transaction.commit();
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/frag_container"
android:layout_height="fill_parent"
android:orientation="horizontal">
</LinearLayout>
frag_one.xml と frag_two.xml は似ています
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One" />
</LinearLayout>
何が問題なのかわかりません...1つのフラグメントを追加する例をたくさん見ました。