Frage

Ich versuche, einen Musikspieler zu erstellen, der in all meinen Aktivitäten sichtbar sein wird. Um dies zu tun, werde ich a verwenden Fragment Wie einige von Ihnen mich früher beraten haben.

Da habe ich keine Erfahrung mit Fragments Was auch immer, ich beschloss, zuerst ein Fragment in einer "Füllstoff" -Aktivität umzusetzen. Ich habe ein einfaches Fragment erstellt, das nur eine Schaltfläche und einen Text enthielt, und versuchte, dies in meiner Füllstoffaktivität aufzublasen.

Nach dem Aufblasen dieses Fragments zeigt sich jedoch nicht. Eine Nachricht wird auf Logcat gedruckt, in dem mir das Fragment aufgeblasen ist.

Ich bin mir ziemlich sicher, dass mir etwas fehlt, aber da ich keine Erfahrung damit habe und kein Tutorial finden konnte, das ganz erklärt, wie ich das machen soll, weiß ich nicht, was ich vermisse.

Musikspieler Fragment

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);
        }


}

Musik Player -Layout

<?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>

Füllstoffaktivität

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();
            }
        });

    }

}

Fülllayout

<?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>

Offensichtlich mache ich etwas falsch, aber was?

Notiz: Ich verwende Android 2.3 mit der Kompatibilitätsbibliothek.

Jede Hilfe wird geschätzt


Das Problem wurde mit Hilfe von Yashwanth Kumar und Blesgenm behoben. Einer gab eine XML -Lösung, die andere eine programmatische Lösung. Jetzt frage ich mich nur, welche Lösung die wünschenswerteste ist. Gibt es mit beiden Lösungen Zugeständnisse, oder kocht alles auf die bevorzugte Lösung des Programmierers?

War es hilfreich?

Lösung

Ich habe nicht versucht, Fragmente direkt zu XML hinzuzufügen. Aber ich mache normalerweise einen Framelayout oder Linearlayout zum Füllstofflayout.

Angenommen, seine ID ist 'Fragment_Holder'. Ich verwende den folgenden Code in der Fragmentaktivität direkt nach SetContentView. Versuche dies

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new AppMusicPlayer(),"musicplayer");
ft.commit();

Andere Tipps

Sie sollten Layout_Height als 0dip erwähnen, nicht die Breite im vertikalen linearen Layout.

Ändern Sie, dass es funktionieren sollte.

<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" />
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top