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

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

问题描述

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

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

我正在尝试:

  1. 摆脱项目之间的分隔线.我想这可以从样式中定义,但是我不知道怎么做.我试图得到 在运行时调用首选项ListView findViewById(android.R.id.list),正如我在某处阅读的内容,但是 返回null.

  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.

在页眉顶部设置新的,全宽分隔线,如

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中添加自定义首选项:
  • Removing padding from list;
  • Adding a custom preference in my XML:

n

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

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

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