- Forum-Beiträge: 6
15.05.2015, 16:27:19 via Website
15.05.2015 16:27:19 via Website
Hallo zusammen,
das Thema Fragments ist für mich noch relative neu, deshalb ist beim Problem vielleicht sehr trivial
Ich habe ein default-layout, welches ich beim Klick auf ein Icon mit einem Fragment überlagern/austauschen möchte.
Wenn ich jedoch den Replace durchführe, sind alle Elemente des default layouts bis auf die Buttons überdeckt. Ich habe schon gegoogelt, jedoch nichts nützliches finden können wieso die Buttons bestehen bleiben...weiß jemand Rat?
hier die entsprechenden Code-Snippes. Namespaces habe ich entfernt, da man als neues Mitglied hier wohl keine "externen Links" angeben darf
UPDATE: Ich habe nun herausgefunden, dass es wohl daran liegt, dass ich die Buttons in der XML anlegen, das Fragement jedoch erst zur Laufzeit. Wie füge ich das Fragement korrekt in die XML-Datei des Layouts ein damit diese den dort alles bis auf den NavigationDrawer überdeck?
das default-layout:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:id="@+id/numberField"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:hint="@string/enter_number"
android:inputType="number"
android:maxLength="3"
android:digits="0123456789" />
<TextView
android:id="@+id/staticText"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:text="@string/guess_info"
android:layout_below="@+id/numberField"
android:layout_marginTop="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_marginTop="10sp"
android:layout_below="@+id/staticText"
android:layout_width="fill_parent"
android:layout_height="130sp">
</ImageSwitcher>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/submit_button"
android:id="@+id/submit_button"
android:layout_above="@+id/reset_button"
android:background="@android:color/white"
android:layout_marginBottom="10sp"
android:onClick="handleGuess" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/reset_button"
android:id="@+id/reset_button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/buttons_color"
android:textColor="@drawable/button_text"
android:onClick="restartGame" />
</RelativeLayout>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@drawable/nav_drawer_bg"/>
Das Layout des Fragments
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/custom_blue" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/history_headline"
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10sp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/history_list_view"
android:layout_below="@+id/textView"
android:choiceMode="singleChoice"></ListView>
Die Fragement Klasse
i
mport android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class HistoryFragment extends Fragment {
OnItemSelectedListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnItemSelectedListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_history, container, false);
}
public interface OnItemSelectedListener {
public void setText(String text);
}
}`
der switch in der activity
f
ragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_placeholder, historyFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();`
Ich hoffe das reicht an Code aus
— geändert am 15.05.2015, 17:19:03
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.