android set divider padding for preference screen

后端 未结 4 1646
清歌不尽
清歌不尽 2021-01-05 04:37

I have PreferenceScreen contain many CheckBox , i customize it by refer it to custom layout as bellow :

 
 &         


        
4条回答
  •  春和景丽
    2021-01-05 05:24

    You must create your own drawable resource for divider:

    
    
    
    
        
    
             
    
             
    
        
    
    
    

    In this case divider shape height is 4dp and top padding is 5dp, when you set divider height you must sum shape height + inset's.

    ListView list = (ListView) findViewById(android.R.id.list);
    list.setDivider(new ColorDrawable(Color.RED)); // or some other color int
    list.setDividerHeight((5)); //wrorng code
    list.setVerticalScrollBarEnabled(false);
    

    To set divider height correctly for all dencity screens you must set it in dp's but for default number "5" interpreted in pixels(px).

    final float scale = getContext().getResources().getDisplayMetrics().density;
    int pixels = (int) (dps * scale + 0.5f);
    

    For more information about shape and inset drawable resource use Android Documentation Drawable resources

提交回复
热议问题