Custom PreferenceCategory Headings

前端 未结 4 1016
耶瑟儿~
耶瑟儿~ 2020-11-28 19:45

I have a simple preference screen defined like this


    

        
4条回答
  •  庸人自扰
    2020-11-28 20:26

    Styling it as described in inazaruk's answer is simple enough but it only changes the style of the text in the heading, it doesn't offer a way to specify a whole new layout (the style will not apply the layout item). There is, however, a straightforward solution if you extend the class:

    public class MyPreferenceCategory extends PreferenceCategory {
    
      public MyPreferenceCategory(Context context) {
        super(context);
        setLayoutResource(R.layout.yourlayout);
      }
    
      public MyPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayoutResource(R.layout.yourlayout);
      }
    }
    

    and simply use this instead of the original PreferenceCategory when defining your Preferences layout.

    Your layout can, of course, have anything you like, including lines above or below the text, different backgrounds, padding, whatever. For instance, this will show a material design colored subtitle with a line above:

    
    
        
        
    
    

提交回复
热议问题