Ich versuchte, die Hintergrundfarbe eines Fragments zu ändern, aber ein kleines Problem trat auf.Android: Ändern der Hintergrundfarbe des Fragments
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
So oben gezeigt, ist der Code, den ich für meine Hauptklasse hatte, die die XML-Datei für das Fragment nennt.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment1"
android:name="com.northreal.practice.FirstFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#CBA" />
</LinearLayout>
Above ist das main.xml Layout, das von der Hauptklasse (MainActivity) genannt wird.
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, parent, false);
}
}
Above die XML-Datei mit dem Fragment nennt diese Klasse.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLAHHHH"
android:layout_gravity="center_vertical" />
</LinearLayout>
Dieses Layout oben durch die Klasse FirstFragment aufgeblasen wird
Also, warum sich das ändern die Farbe des Hintergrundes meines Fragment eigentlich nicht?
Dank, dass mein Problem gelöst, sondern von getView() Ich habe getRootView() verwendet: fragment.getRootView(). setBackgroundColor (Color.WHITE); – zarej
Ich bekomme eine NullPointerException, wenn ich dies auf einem PreferenceFragment = s versuche – Solace