自定义首选项标题布局 [英] Customizing preference headers layout

查看:117
本文介绍了自定义首选项标题布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用依赖于AppCompatDelegate的AndroidStudio生成的SettingsActivity(新建活动设置活动).我已经成功添加了一个工具栏,并为用于显示程序首选项标题的listview设置了一个自定义分隔符,如下所示:

I am using the SettingsActivity generated from AndroidStudio (New, Activity, Settings Activity) that relies on AppCompatDelegate. I have succeeded to add a toolbar and to set a custom divider to the listview used for displayer preference headers as follows:

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

    LinearLayout root =
            (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.toolbar, root, false);
    root.addView(toolbar, 0);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    ListView listView = getListView();
    listView.setDivider(ContextCompat.getDrawable(this, R.drawable.settings_divider));
    listView.setDividerHeight(1);

    setupActionBar();
}

但是,我既不知道如何增加与每个列表视图项相关的填充,又不知道如何增加图标与首选项标题之间的间距.是否可以使用样式或通过自定义布局?

However, I cannot figure out how to increase the padding assocatied to each listview item neither how to increase spacing between the icon and the preference header title. Is it possible using styles or by passing a custom layout?

推荐答案

您可以创建一个覆盖PreferenceCategory的自定义布局.例如这样的

You can create a custom layout that overrides PreferenceCategory. For example like this:

public class CustomPreferenceCategory extends PreferenceCategory {

    public CustomPreferenceCategory(Context context) {
         super(context);
    }

    public CustomPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        View view = super.onCreateView(parent);

        return view;
    }

    @Override
    public View getView(View convertView, ViewGroup parent) {
        final View view = super.getView(convertView, parent);
        view.setPadding(view.getPaddingLeft(), view.getPaddingTop()*2, view.getPaddingRight(), 10);

        TextView textView = (TextView) view.findViewById(android.R.id.title);
        textView.setGravity(Gravity.BOTTOM);

        return view;
    }
}

通过覆盖这些方法(如果需要,可以设置更多方法,可以设置填充,边距,背景色等...

By overriding these methods (and some more if you want you can set a padding, margin, backgroundcolor, etc...

然后使用CustomPreferenceCategory代替PreferenceCategory

当然,您可以使用Preference做同样的事情(我不确定您是要使用自定义标题还是自定义列表项-类别将是标题,而首选项是普通列表项)

Of course you can do the same with a Preference (I was not sure if you want custom headers or custom list items - the category will be the header and the preference is a normal list item)

这篇关于自定义首选项标题布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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