如何管理 PreferenceFragment 中的分隔符? [英] How to manage dividers in a PreferenceFragment?

查看:20
本文介绍了如何管理 PreferenceFragment 中的分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在 PreferenceFragment 中处理首选项.这是我所拥有的:

I started dealing with preferences in a PreferenceFragment. Here's what I have:

我正在尝试:

  1. 去掉项目之间的分隔线.我想这可以从样式中定义,但我不知道如何定义.我试着得到在运行时调用偏好 ListViewfindViewById(android.R.id.list),正如我在某处读到的,但它返回空值.

  1. get rid of the dividers between items. I suppose this can be defined from styles, but I can't figure out how. I tried getting the preference ListView at runtime calling findViewById(android.R.id.list), as I read somewhere, but it returns null.

在标题的正上方设置新的全宽分隔线,如此处.例如,在这种情况下,我希望在Statistiche"上方有一个全宽分隔线,但不在列表顶部的Generali"上方.

set new, full width dividers right on top of the headers, as seen here. For example in this case I want a full width divider right above "Statistiche", but not above "Generali" which is on top of the list.

我想到的唯一方法是将分隔符设置为虚假偏好,例如:

The only way that comes to my mind is setting dividers as fake preferences, like with:

<Preference
    android:layout="@layout/divider" //here I set width and a divider resource
    />

<PreferenceCategory ... />

这里的主要问题是我的PreferenceFragment(或它所在的ActionBarActivity)有一些左/右填充,这使得我添加到preferences.xml中的任何分隔符<强>不覆盖整个宽度.

The main issue here is that my PreferenceFragment (or the ActionBarActivity it's in) has some left/right padding, that make any divider I add into preferences.xml not cover the entire width.

所以我的问题是:

  • 如何去掉您在图像中看到的默认项-项分隔符?

  • How can I get rid of default, item-item dividers that you can see in the image?

如何在标题正上方设置全宽分隔线,或者如何摆脱内部片段/活动填充?当然,我的活动布局没有任何(显式)填充.

How can I set full width dividers right above headers, or how can I get rid of internal fragment/activity padding? Of course my activity layout has no (explicit) padding whatsoever.

推荐答案

完全忘记了这个问题,现在发布一个答案来帮助其他人.我通过在承载我的 PreferenceFragment 的活动的 onResume() 方法中移动我的代码来解决这个问题.我认为还有其他几个点可以使用 findViewById(android.R.id.list) 调用非空 ListView.

Had totally forgot about this question, will post an answer now to help others. I solved by moving my code in the onResume() method of the activity that hosts my PreferenceFragment. I think there are several other points at which you can recall a non-null ListView using findViewById(android.R.id.list).

public boolean mListStyled;

@Override
public void onResume() {
    super.onResume();
    if (!mListStyled) {
        View rootView = getView();
        if (rootView != null) {
            ListView list = (ListView) rootView.findViewById(android.R.id.list);
            list.setPadding(0, 0, 0, 0);
            list.setDivider(null);
            //any other styling call
            mListStyled = true;
        }
    }
}

您可能可以去掉 rootView 检查,但同时您甚至可能想要检查 list != null.反正我没有遇到过这样的 NPE.

You can probably get rid of the rootView check, but at the same time you might even want to check for list != null. I didn't face any NPE this way anyway.

因此,setDivider(null) 取消了项-项分隔符.我设法通过以下方式添加了覆盖整个屏幕宽度的部分分隔符:

So, setDivider(null) takes off the item-item dividers. I managed to add section dividers covering the full width of the screen by:

  • list中移除内边距;
  • 在我的 XML 中添加自定义首选项:

n

 <Preference
     android:title="divider"
     android:selectable="false"
     android:layout="@layout/preference_divider"/>

这篇关于如何管理 PreferenceFragment 中的分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆