PreferenceFragment is shown transparently

守給你的承諾、 提交于 2019-12-05 00:25:29

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.

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

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

In your ReaderPreferences implementation add:

public void onResume() {
    super.onResume();
    getView().setBackgroundColor(Color.WHITE);
}

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

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.

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

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.

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

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> 

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!