Android layer-list(边框加粗效果)

来源:互联网 发布:myeclipse配置优化 编辑:程序博客网 时间:2024/05/20 22:35


要实现以上效果:当checkbox被选中之后,checkbox的边框被加粗(颜色改变、字体改变可容易实现,不再赘述)。暂时的解决方案是使用layer-list。

layer-list:图层的累加,字面上是多个层,也就是你可以把多个layer放在一起,然后一其显现出来。


tab-widget中自定义的视图checkbox所处布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="40dp"    android:orientation="vertical">    <CheckBox        android:id="@+id/checkMsg"        style="@style/tab_checkbox"        android:layout_width="match_parent"        android:layout_height="40dp" /></LinearLayout>

checkbox style.xml 样式文件:

<style name="tab_checkbox">        <item name="android:background">@drawable/msg_tab_widget_sharp</item>        <item name="android:button" >@null</item>        <item name="android:textColor">@color/gray</item>        <item name="android:textSize" >16sp</item>        <item name="android:gravity" >center</item>    </style>

msg_tab_widget_sharp.xml checkbox style.xml 样式文件中的background 的布局文件:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/msg_tab_widget1" android:state_checked="true"></item>    <item android:drawable="@drawable/msg_tab_widget2" android:state_checked="false"></item>    <item android:drawable="@drawable/msg_tab_widget2"></item></selector>

msg_tab_widget1.xml  当checkbox被选中时:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item>       <!-- This is the border color -->        <shape>            <solid android:color="#0066CC" />        </shape>    </item>    <item android:bottom="3dp" android:right="1dp">       <!-- This is the main color -->        <shape>            <solid android:color="#E9F2FC" />            <padding                android:bottom="5dp"                android:left="5dp"                android:right="5dp"                android:top="5dp" />        </shape>    </item></layer-list>

msg_tab_widget2.xml  当checkbox未被选中时,即默认显示的状态:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item>        <!-- This is the border color -->        <shape>            <solid android:color="#0066CC" />        </shape>    </item>    <item        android:bottom="1dp"        android:right="1dp">        <!-- This is the main color -->        <shape>            <solid android:color="#E9F2FC" />            <padding                android:bottom="5dp"                android:left="5dp"                android:right="5dp"                android:top="5dp" />        </shape>    </item></layer-list>





其他使用demo:如在 android:style/Widget.ProgressBar.Large 或者其他的progressbar风格

<style    name="progressBarStyleSmall"    parent="android:style/Widget.ProgressBar.Large" >    <item name="android:indeterminateDrawable">@drawable/progress</item></style><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- <item android:drawable="@android:drawable/progress_circular_background" /> -->    <!--      <item>          <shape android:shape="ring" android:innerRadiusRatio="3.4" android:thicknessRatio="6.0">             <gradient android:useLevel="true" android:type="sweep" android:startColor="#ff000000" android:endColor="#ffffffff" />          </shape>      </item>    -->    <item>        <rotate            android:drawable="@drawable/load2"            android:fromDegrees="0"            android:pivotX="50%"            android:pivotY="50%"            android:toDegrees="370" />    </item></layer-list>




原创粉丝点击