【问题标题】:PreferenceFragment with transparent background?PreferenceFragment 具有透明背景?
【发布时间】:2014-04-28 21:25:36
【问题描述】:

我创建了一个包含两个类别和一个复选框的 PreferenceFragment,但是,当我在我的应用程序中显示它时,背景似乎是透明的。

我可以看到主要活动、字段和 PreferenceFragment 位于主要活动的顶部...解决方案是什么?

在我的主要活动中,我这样做是为了在选择设置按钮时打开 PreferenceFragment:

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

我的 PreferenceFragment 代码如下所示: 公共类 SettingsFragment 扩展 PreferenceFragment {

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

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

我的preferences.xml 看起来像这样:

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

我尝试了 Stack Overflow 上的另一个答案建议并将其添加到我的 Preference Fragment 中,但是,虽然它阻止了 Main Activity 可见的字段,但它似乎只是一个掩码,因为片段字段也受到影响我设置的颜色:

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

谢谢你的帮助!!!

编辑:我想我在打字时回答了我自己的问题...我补充说:

addPreferencesFromResource(R.xml.preferences);

做完之后:

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

这是解决这个问题的正确方法吗?

【问题讨论】:

    标签: android android-fragments android-activity android-preferences preferenceactivity


    【解决方案1】:

    将此添加到扩展 PreferenceFragment 的片段(SettingsFragment)中:

    @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】:

      只是不要同时使用FragmentManagerSupportFragmentManager ...在替换片段时使用两者之一。希望它会工作

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

      如果您使用的是PreferenceFragment,那么您必须使用getFramgentManager() 而不是getSupportFragmentManager();

      【讨论】:

      • 或者干脆使用 PreferenceActivity
      【解决方案3】:

      我可以避免这些问题的唯一方法是使用 preferenceActivity 类: How do you create Preference Activity and Preference Fragment on Android? 来自 WannaGetHigh

      【讨论】:

        【解决方案4】:

        我会加两分钱,因为我遇到了与 PreferenceFragmentCompat 类似的问题,这是 google 的最佳结果,但这并没有完全回答我的问题。

        我遇到了同样的问题,使用 PreferenceFragmentCompat 可以工作,但设置背景是透明的,您仍然可以在底层 activty 中看到视图 - 在 getSupportFragmentManger 中将 .add() 更改为 .replace() 会产生相同的覆盖。

        改变背景颜色的解决方案奏效了。但是,对于我的构建,不推荐使用 getColor() 方法。我改为使用以下代码。

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

        这解决了透明度问题,除了一个按钮仍然可以点击。为简洁起见,问题是我试图替换包含活动视图的布局。

        最终的工作是以最高顺序创建一个空布局并在该布局上使用 .replace() 。这些按钮现在被覆盖,不再可点击。

        XML,其中按钮在首选项片段上方仍然可见。

        <?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。

        <?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】:

          使用 Kotlin 时,将以下代码添加到 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】:

            尝试将所有 Fragment 导入更改为 android.app.Fragment 而不是 android.support.v4.app.Fragment。另外,请确保您使用 getFragmentManager() 而不是 getSupportFramentManager() 访问您的片段管理器。

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

            【讨论】:

            • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
            • 是的,你是对的。我想我只是懒惰。进一步充实了答案。
            猜你喜欢
            • 1970-01-01
            • 2018-05-31
            • 2020-10-29
            • 2011-07-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多