CheckBox 和tab的自定义样式

打开源码中CheckBox.java文件,我们可以看到如下内容:
public class CheckBox extends CompoundButton {
    public CheckBox(Context context) {
        this(context, null);
    }
    
    public CheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.checkboxStyle);
    }

    public CheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}
可以看到,CheckBox是继承于CompoundButton,而CompoundButton:
public abstract class CompoundButton extends Button implements Checkable
是Button的一个子类,由此可知,CheckBox也为Button的一个子类。从上面的代码中可以看出,CheckBox的不同之处就是添加了属性checkboxStyle,我们在源码中找到该样式的定义如下:
<style name="Widget.CompoundButton.CheckBox">
        <item name="android:background">@android:drawable/btn_check_label_background</item>
        <item name="android:button">@android:drawable/btn_check</item>
    </style>
其中,在资源文件中找到btn_check_label_background,该图片是背景透明没有内容的一张图片,因此CheckBox的显示就是由btn_check决定,我们打开btn_check.xml文件,可以看到以下内容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Enabled states -->
        
    <item android:state_checked="true" android:state_window_focused="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off" />

    <item android:state_checked="true" android:state_pressed="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off_selected" />

    <item android:state_checked="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off" />
    <item android:state_checked="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on" />


    <!-- Disabled states -->

    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/btn_check_on_disable" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/btn_check_off_disable" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_check_on_disable_focused" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_check_off_disable_focused" />

    <item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" />
    <item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" />

</selector>
每一个item就对应着CheckBox在不同的选择状态下显示的效果,了解该文件的内容后,我们就可以更具自己的需要来设计美观的CheckBox。RadioButton的显示与CheckBox基本相似,可以仿照CheckBox加以修改。

 

转载:

http://blog.csdn.net/zhll3377/article/details/7287124

 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1.制作4个9patch的tab样式,可参考android默认的资源

tab_unselected.9.png tab_selected.9.pngtab_press.9.pngtab_focus.9.png

这4个资源分别代表Tab的4种状态。
.定义Tab的selector样式(就叫它tab_indicator.xml好了),将其放入drawable文件夹下,代码如下:

 

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <!-- Non focused states -->  
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />  
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />  
    <!-- Focused states -->  
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />  
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />  
    <!-- Pressed -->  
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" />  
</selector>   
.编写indicator的布局文件(不妨也叫tab_indicator.xml),将其放入layout文件夹下,代码如下:

 

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="0dip"  
    android:layout_height="64dip"  
    android:layout_weight="1"  
    android:layout_marginLeft="-3dip"  
    android:layout_marginRight="-3dip"  
    android:orientation="vertical"  
    android:background="@drawable/tab_indicator">  
    <ImageView android:id="@+id/icon"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
    />  
    <TextView android:id="@+id/title"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true"  
        style="?android:attr/tabWidgetStyle" mce_style="?android:attr/tabWidgetStyle"  
    />   
.接下来就是在TabActivity中使用我们自己编写的Tab样式了:

 

// 首先获取TabWidget  
mTabHost = getTabHost();  
LinearLayout ll = (LinearLayout)mTabHost.getChildAt(0);  
TabWidget tw = (TabWidget)ll.getChildAt(0);   

然后用类似如下代码创建TabSpec,就大功告成了。

 

RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator, tw, false);  
TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);  
tvTab1.setText("tab1");  
mTabHot = mTabHost.newTabSpec("TAB_1")  
        .setIndicator(tabIndicator1)  
        .setContent(contentIntent);

 

http://www.cnblogs.com/tt_mc/archive/2010/10/07/1845090.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值