Change the color of divider in LinearLayout

随声附和 提交于 2019-12-05 22:02:11

问题


May I know how I can change the color of divider in LinearLayout?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="?android:attr/dividerVertical"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ff2d2d2d" >
...
</LinearLayout>

Do I need to manually copy 9 patch image from Android SDK into my project, and define my own attribute to refer it?


回答1:


It looks like android:divider attribute doesn't accept a color value. So you have to create a separate divider drawable in order to get it works:

divider.xml

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

    <size android:width="1dip" />
    <solid android:color="#f00" />

</shape>

layout.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="@drawable/divider"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ff2d2d2d" >

Also, please note that android:divider is only available in android 3.0 or higher and it doesn't work in previous android versions.




回答2:


This is how I did it

      <ImageView
            android:id="@+id/imgVwmarkupborder"
            android:layout_width="280dp"
            android:layout_height="2dp"
            android:src="@android:color/white" />



回答3:


<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/white"/>

In my method i am use this one..



来源:https://stackoverflow.com/questions/15170790/change-the-color-of-divider-in-linearlayout

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