问题
I'm trying to show a PreferenceFragment
after I select the Preferences option in my ActionBar.
However, after replacing current content with the PreferenceFragment
you can see the old content below it. As in, you can see straight through the preferences.
Am I missing something here? I used an example from a book I own, which didn't use any layout files for the preferences. Do you need those?
Used code:
Actionbar menu
private boolean MenuChoice(MenuItem item) {
switch (item.getItemId()) {
case 0:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
ReaderPreferences prefs = new ReaderPreferences();
fragmentTransaction.replace(android.R.id.content, prefs);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;
}
return false;
}
PreferenceReader
public class ReaderPreferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// --load the preferences from an XML file---
addPreferencesFromResource(R.xml.preference);
}
}
Actual result:

As you can see, you look straight through my preferences. What did I do wrong?
回答1:
Create your PreferenceFragment.java class like this:
public class UserPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setBackgroundColor(Color.BLACK);
getView().setClickable(true);
}
}
the trick is:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setBackgroundColor(Color.BLACK);
getView().setClickable(true);
}
EDIT:
Edited as suggested by JDenais, even if it's not strictly necessary to the topic.
回答2:
I had the same problem and solved it //without having to start a new activity//. This method has the advantage that your main activity doesn't go through a pause-resume cycle. The key is to have your main UI as a fragment, and then hide it when the Pref fragment is called. The main fragment can be included statically or dynamically.
FragmentTransaction ft = getFragmentManager().beginTransaction();
AppSettingsFragment prefs = new AppSettingsFragment();
// This adds the newly created Preference fragment to my main layout, shown below
ft.add(R.id.main_layout,prefs);
// By hiding the main fragment, transparency isn't an issue
ft.hide(mMyMainFragment);
ft.addToBackStack(null);
ft.commit();
The main_layout.xml
looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Uncomment for static fragment inclusion -->
<!-- fragment android:name="com.legynd.ui.MyMainFragment"
android:id="@+id/mainfragment"
android:layout_width="match_parent"
android:layout_height="match_parent" / -->
</RelativeLayout>
回答3:
The best and most portable solution is answered here
Which is:
Adding the following code to your PreferenceFragment will let you add a background color, image, etc.
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.your_color));
return view; }
回答4:
In your ReaderPreferences implementation add:
public void onResume() {
super.onResume();
getView().setBackgroundColor(Color.WHITE);
}
回答5:
Use an empty layout for your activity like this:
activity_settings.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
then in activity's onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ReaderPreferences mReaderPreferences = new ReaderPreferences ();
getSupportFragmentManager().beginTransaction()
.replace(R.id.settingsFragment, mReaderPreferences ).commit();
}
回答6:
The background color can be set to the Fragment View in onStart(). As below:
public class ReaderPreferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
@Override
public void onStart() {
super.onStart();
View v = getView();
v.setBackgroundColor(getActivity().getResources.getColor(R.color.my_color));
}
This works for me on all of my test devices.
The accepted answer is not the answer to what the question states here. In fact, its a workaround which creates a container activity for the preference fragment.
回答7:
Add this in your Fragment that extends PreferenceFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.white));
return view;
}
回答8:
Eventually fixed it with a very easy fix.
I simply called the PreferenceFragment
in a new Intent
, and it worked perfectly.
For anyone with the same problem:
Prefs.java
public class ReaderPreferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// --load the preferences from an XML file---
addPreferencesFromResource(R.xml.preference);
}
In my main screen when pressing a button:
Intent i = new Intent(this, Prefs.class);
startActivity(i);
That's all. After setting the preferences simply press the back button and you're done.
回答9:
I was also stuck with the exact same issue, and after trying out a couple of things I found our that it was happening because in my case I had mixed up two different versions of Fragments and FragmentManagers.
The onCreate of my MainActivity looked like this:
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
However when I wanted to replace this MainFragment with my own PreferenceFragment, I tried using something similar to what you used:
getFragmentManager().beginTransaction()
.replace(R.id.container, new SettingsFragment())
.addToBackStack(null)
.commit();
The way I understand it is that, you should use the same type of FragmentManager for replace to work, so I just had to go ahead and make appropriate changes to the way I wired up the MainFragment making it look like this:
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
Ofcourse, I had to refactor MainFragment to extend from android.app.Fragment
instead of android.support.v4.app.Fragment
回答10:
I tried Shayan_Aryan's solution, but my app was dying with error:
Your content must have a ListView whose id attribute is 'android.R.id.list'
So I had to create a LinearLayout with no text in the empty case. Well I suppose there is a more elegant solution, but this was the only one that worked to me.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
回答11:
i used many techniques but no gain
Finally i fixed it by adding background of the container or the fragment layout like this
android:background="@android:color/white"
you just need to add in your Layout or the container view of you Fragment
Hope it will help
来源:https://stackoverflow.com/questions/8362908/preferencefragment-is-shown-transparently