当前位置:操作系统 > 安卓/Android >>

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

 


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

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

 


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


[html]
<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> 

<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 样式文件:


[html]
<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> 

<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 的布局文件:

 

[html]
<?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> 

<?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被选中时:


[html]
<?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> 

<?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 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,