android set divider padding for preference screen

后端 未结 4 1615
清歌不尽
清歌不尽 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:03

    You should try deriving a theme from android's default one, assigning it to your preference screen (although they say there's a bug on 2.x versions that prevents you from doing that unless you workaround as described here), and overriding the android:dividerPadding property of one of the styles.

    I can't really tell you for sure which style that would be, you'll have to dig into it, here's android's styles.xml for reference and here's an example how to override ListView appearance in a theme

    0 讨论(0)
  • 2021-01-05 05:05

    To allow preference divider padding

    first : add this line to your preference activity which lead to transparency of android default divider

    list.setDivider(new ColorDrawable(0x00000000));
    

    second : create folder named drawable in res , then create on it divider.xml which will be as below :

    <?xml version="1.0" encoding="utf-8" ?> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android">
       <solid android:color="#B22222" /> 
     </shape>
    

    third :

    add View to your mylayout.xml so it will be as below :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="@dimen/preference_icon_minWidth"
        android:orientation="horizontal">
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>
    
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dip"
        android:layout_marginRight="8dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">
    
        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:textAppearance="?android:attr/textAppearanceMedium"           
            android:fadingEdge="horizontal" />
    
        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />
    
        <View 
           android:id="@+id/divider" 
           android:background="@drawable/divider" 
           android:layout_width="match_parent" 
           android:layout_marginLeft="30dp" 
           android:layout_marginRight="30dp" 
           android:layout_height="5dp" 
           android:layout_below="@+android:id/summary"/> 
    
    </RelativeLayout>
    
    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minWidth="@dimen/preference_widget_width"
        android:gravity="center"
        android:orientation="vertical" />
    

    the point here you add View bellow your texts and refer that view to divider shape in drawable res so finally you will get an custom divider which it can be customized as you need.

    hope that help you .

    the output will be as below:

    enter image description here

    0 讨论(0)
  • 2021-01-05 05:14

    You have to create your custom drawable-shape

    custom_divider.xml

    <shape android:shape="rectangle">
        <solid android:color="@color/grey" />
        <corners android:radius="2dp" />
    </shape>
    

    set this as divider for your ListView in xml

    activity_list.xml

    <ListView
        android:dividerHeight="2dp"
        android:divider="@drawable/custom_divider"
        ...
    />
    
    0 讨论(0)
  • 2021-01-05 05:24

    You must create your own drawable resource for divider:

    <?xml version="1.0" encoding="utf-8"?>
    
    <inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetTop="5dp">
    
        <shape android:shape="rectangle">
    
             <solid android:color="#FF0000" />
    
             <size android:height="4dp" />
    
        </shape>
    
    </inset>
    

    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

    0 讨论(0)
提交回复
热议问题