PreferenceFragment with transparent background?

风流意气都作罢 提交于 2020-07-06 09:50:39

问题


I have created a PreferenceFragment with two categories and one checkbox, however, when I display it in my app, the background appears to be transparent.

I can see the main activities, fields and the PreferenceFragment ones are laid over top of the ones from the main activity...what is the solution to this?

In my main activity I am doing this to open the PreferenceFragment when the settings button is selected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //handle presses on action bar items
    switch (item.getItemId()) {
        case R.id.action_settings:
            getFragmentManager().beginTransaction().replace(android.R.id.content,
                    new SettingsFragment()).commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

My PreferenceFragment code looks like this: public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
  }
}

My preferences.xml looks like this:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout="@layout/fragment_settings">
  <PreferenceCategory
    android:title="@string/server_prefs_title"
    android:key="pref_key_server_settings" >
  <CheckBoxPreference
    android:key="pref_server_url"
    android:title="@string/pref_server_url"
    android:summary="@string/pref_server_url_summ"
    android:defaultValue="true" />
  </PreferenceCategory>
  <PreferenceCategory
    android:title="@string/company_prefs_title"
    android:key="pref_key_company_settings" >
  </PreferenceCategory>
</PreferenceScreen>     

I tried what another answer on Stack Overflow suggested and added this to my Preference Fragment, however, although it blocks the fields visible from the Main Activity, it just appears to be a mask, because the fragment fields are also being affected by the color that I set:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  
 savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(android.R.color.black));

    return view;
}

Thanks for the help!!!

EDIT: I think I answered my own question while typing this out...I added:

addPreferencesFromResource(R.xml.preferences);

After doing:

 View view = super.onCreateView(inflater, container, savedInstanceState);
 view.setBackgroundColor(getResources().getColor(android.R.color.black));

Is that the proper way to do solve this?


回答1:


Add this in your Fragment (SettingsFragment) 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;
}



回答2:


Just don't use both FragmentManager and SupportFragmentManager ... use one of the two when your are replacing fragments. Hope it will work

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

If you are using PreferenceFragment then you'll have to use getFramgentManager() instead of getSupportFragmentManager();.




回答3:


Only way I could avoid those issues was to use a preferenceActivity class: How do you create Preference Activity and Preference Fragment on Android? by WannaGetHigh




回答4:


I'll add my two cents since I had a similar issue with PreferenceFragmentCompat and this is the top result from google, but this didn't quite answer my question.

I had the same issue where using PreferenceFragmentCompat would work, but the settings background was transparent and you could still see views in the underlying activty - changing .add() to .replace() in the getSupportFragmentManger yielded the same overlay.

The solution of changing the background color worked. However, for my build the getColor() method was deprecated. I instead use the following code.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getListView().setBackgroundColor(Color.WHITE);
}

This solved the transparency issue except for one button, which was still clickable. For the sake of brevity, the issue was I was trying to replace the layout that contained the activity views.

What ended up working was creating an empty layout at the highest order and using .replace() on that layout. The buttons are now covered and no longer clickable.

XML where button was still visible above preferences fragment.

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

     <!--I was replacing this layout that had all the activity views-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent">

        <TextView
            android:text="Z"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/earth_acc_z"
            app:layout_constraintTop_toBottomOf="@+id/earth_acc_y"
            app:layout_constraintStart_toStartOf="@+id/earth_acc_y"
            app:layout_constraintEnd_toEndOf="@+id/earth_acc_y"
            app:layout_constraintHorizontal_bias="1.0"/>
        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/toggleRecording"
            app:layout_constraintStart_toStartOf="@+id/toggleRecording" 
            android:layout_marginStart="8dp"/>

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

XML of working example with new empty constraint.

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--FrameLayout with two nested ConstraintLayouts-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent">

        <!--ConstraintLayout with acitivty views-->
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/frameLayout">

            <TextView
                android:text="Z"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/earth_acc_z"
                app:layout_constraintTop_toBottomOf="@+id/earth_acc_y"
                app:layout_constraintStart_toStartOf="@+id/earth_acc_y"
                app:layout_constraintEnd_toEndOf="@+id/earth_acc_y"
                app:layout_constraintHorizontal_bias="1.0"/>
            <Button
                android:text="Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button" android:layout_marginTop="8dp"
                app:layout_constraintTop_toBottomOf="@+id/toggleRecording"
                app:layout_constraintStart_toStartOf="@+id/toggleRecording" 
                android:layout_marginStart="8dp"/>

        </android.support.constraint.ConstraintLayout>

        <!--Preference fragment should replace this empty layout-->
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/preferenceFragment"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </android.support.constraint.ConstraintLayout>
    </FrameLayout>
</android.support.constraint.ConstraintLayout>



回答5:


When using Kotlin add the following code to your SettingsFragment:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = super.onCreateView(inflater, container, savedInstanceState)
    view?.setBackgroundColor(Color.WHITE)
    return view
}



回答6:


Try changing all your Fragment imports to android.app.Fragment instead of android.support.v4.app.Fragment. Also, make sure that you are accessing your fragmentmanager with getFragmentManager() not getSupportFramentManager().

Reference: http://ausmarton.blogspot.com.au/2014/01/preferencefragment-shows-up-with.html



来源:https://stackoverflow.com/questions/23351455/preferencefragment-with-transparent-background

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