フラグメントは膨らんだ後に表示されません[修正しましたが…
質問
私はすべてのアクティビティで見える音楽プレーヤーを作成しようとしています。これを行うには、aを使用します Fragment
あなたの何人かが以前に私にアドバイスしたように。
経験がないので Fragments
いずれにせよ、私は最初に「フィラー」アクティビティにフラグメントを実装することにしました。ボタンとテキストのみを含む単純なフラグメントを作成し、フィラーアクティビティでそれを膨らませようとします。
ただし、このフラグメントを膨らませた後は表示されません。メッセージは、フラグメントが膨らんでいることを私に言って、ログキャットに印刷されます。
私は何かが足りないと確信していますが、私はこれの経験がなく、これを行う方法をかなり説明したチュートリアルを見つけることができなかったので、私が何が足りないのかわかりません。
音楽プレーヤーの断片
public class AppMusicPlayer extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
System.out.println("fragment added");
return inflater.inflate(R.layout.musicplayer_main, container, false);
}
}
音楽プレーヤーのレイアウト
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the fragment you're looking for" />
</LinearLayout>
フィラーアクティビティ
public class Filler extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filler);
Button b = (Button) findViewById(R.id.terug);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
フィラーレイアウト
<?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" >
<!-- Title bar -->
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/title_bar" >
<TextView
android:id="@+id/fillertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Work In Progress"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" >
</TextView>
<Button
android:id="@+id/terug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="@drawable/button_back"
android:text="Back"
android:textColor="#FFFFFFFF"
android:textStyle="bold" >
</Button>
</RelativeLayout>
<!-- End of title bar -->
<TextView
android:id="@+id/wip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This section of the app has not been made yet. Work in progress." >
</TextView>
<fragment
android:id="@+id/musicPlayerFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:name="com.mobowski.app.player.AppMusicPlayer" />
</LinearLayout>
明らかに私は何か間違ったことをしていますが、何ですか?
ノート: 互換性ライブラリでAndroid 2.3を使用しています。
どんな助けも感謝しています
この問題は、Yashwanth KumarとBlessenmの助けを借りて修正されています。 1つはXMLソリューションを与え、もう1つはプログラマティックソリューションを与えました。今、私はどのソリューションが最も望ましいものであるかを疑問に思っています。どちらかのソリューションで行われる譲歩はありますか、それともプログラマーの優先ソリューションに要約されますか?
解決
XMLに直接フラグメントを追加しようとしませんでした。しかし、私が通常しているのは、FramelayoutまたはLinearLayoutをフィラーレイアウトに追加することです。
IDが「fragment_holder」であるとします。 setContentViewの直後のフラグメントアクティビティで次のコードを使用します。これを試して
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new AppMusicPlayer(),"musicplayer");
ft.commit();
他のヒント
垂直線形レイアウトの幅ではなく、layout_heightを0DIPとして言及する必要があります。
それが機能するべきであることを変えてください。
<fragment
android:id="@+id/musicPlayerFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.mobowski.app.player.AppMusicPlayer" />