Customizing preference headers layout

时光怂恿深爱的人放手 提交于 2019-12-11 12:49:45

问题


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?


回答1:


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

Then use CustomPreferenceCategory instead of PreferenceCategory

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)




回答2:


The adapter of your list should inflate you own custom view inside getView method. In your view set a padding to the content. Much like this

Or You could use this solution: https://stackoverflow.com/a/5309871/6507689



来源:https://stackoverflow.com/questions/38078536/customizing-preference-headers-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!