本文介绍了如何隐藏片段首选项之间的分隔线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想隐藏片段中首选项之间的分隔线.代码如下:

I want to hide the divider between preferences in fragment.The code is below:

1.SettingsActivity.java

1.SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {
    super.onCreate(savedInstanceState);
    SettingsFragment settingsFragement = new SettingsFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(android.R.id.content, settingsFragement, "settings");
    transaction.commitAllowingStateLoss();
}

2.SettingsFragment.java

2.SettingsFragment.java

public class SettingsFragment extends PreferenceFragment{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }
}

3.settings.xml

3.settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory>
        <Preference
             android:key="preference_a"
             android:title="preferenceA"/>
        <Preference
             android:key="preference_b"
             android:title="preferenceB"/>
        <ListPreference
             android:key="list_preference_c"
             android:title="list_preferenceC"/>
    </PreferenceCategory>
</PreferenceScreen>

首选项之间有系统默认分隔符.我想隐藏分隔线.

There are system default dividers amoung the preferences.I want to hide the dividers.

如何隐藏分隔线?非常感谢.

How to hide the dividers? Thanks a lot.

感谢大家的回答.但是,问题是我无法从活动和片段中获取列表视图.

Thanks for everyone`s answer. But,the question is that I cannot get the listview from the activity and fragment.

ListView list = (ListView) findViewById(android.R.id.list);

推荐答案

如果使用的是PreferenceFragmentCompat,则使用的是回收"视图,因此必须使用此代码.这会自动隐藏分隔线

if you are using PreferenceFragmentCompat , it is using Recycle view so you have to use this code . This automatically hides the divider

@Nullable
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

   setDivider(new ColorDrawable(Color.TRANSPARENT));
   setDividerHeight(0);
}

这篇关于如何隐藏片段首选项之间的分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:36