android 自定义TabHost选择器

效果图如下:

实现方式:利用RadioButton单选功能来实现,我们只需要更改选择与为选择时的背景颜色及字体颜色即可。

item_selet_layout文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            style="@style/tabhost_left_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="待审核" />

        <RadioButton
            style="@style/tabhost_center_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="已审核" />

        <RadioButton
            style="@style/tabhost_right_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="我的" />
    </RadioGroup>

</LinearLayout>

TabHostSelector继承LinearLayout:

public class TabHostSelector extends LinearLayout{

	public TabHostSelector(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	private void init(Context context){
		View view = LayoutInflater.from(context).inflate(R.layout.item_select_layout, null);
		RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radiogroup);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				Utils.showToast(getContext(), checkedId+"");
				switch (checkedId) {
				case 1:

					break;
				case 2:

					break;
				case 3:

					break;
				default:
					break;
				}
			}
		});
		radioGroup.check(2);
		LinearLayout.LayoutParams ll = new LayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		this.addView(view,ll);
	}

由于三个选项,左右两个上下角由弧度,于是定义了三个style,分别控制角度。

 <style name="tabhost_center_style" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@null</item>
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/tabhost_textcolor_selector</item>
        <item name="android:background">@drawable/tabhost_center_elector</item>
    </style>
    <style name="tabhost_left_style" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@null</item>
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/tabhost_textcolor_selector</item>
        <item name="android:background">@drawable/tabhost_left_elector</item>
    </style>
    <style name="tabhost_right_style" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@null</item>
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/tabhost_textcolor_selector</item>
        <item name="android:background">@drawable/tabhost_right_elector</item>
    </style>
res/color/color/tabhost_textcolor_selector:控制文字选中时改变成白色,未选中为黑色。
图:

xml代码:

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

    <item android:state_checked="true" android:color="@android:color/white" />
	<item android:color="@android:color/black"></item>
</selector>

 这里设置字体颜色会出现很多问题,期初用其他方式来实现,始终无效,后来看到这种方式,可以实现。在res文件夹下面新建一个color文件夹,把字体颜色selector文件放在里面。

背景色的变换:

tabhost_right_elector.xml

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

    <item android:state_checked="true"><shape>
            <solid android:color="@color/tabhost_selected" />

            <stroke android:width="1dp" android:color="@color/tabhost_selected" />

            <corners android:bottomRightRadius="2dp" android:topRightRadius="2dp" />
        </shape></item>
    <item android:state_checked="false"><shape>
            <solid android:color="@android:color/white" />

            <corners android:bottomRightRadius="2dp" android:topRightRadius="2dp" />

            <stroke android:width="1dp" android:color="@color/tabhost_selected" />
        </shape></item>

</selector>
时间: 2024-08-01 22:53:46

android 自定义TabHost选择器的相关文章

android自定义tabhost,tabcontent用intent获得

地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   

Android 自定义TabHost,TabWidget样式

界面比较简单,要想做得漂亮换几张图片就可以了. 第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id

Android开发之自定义TabHost文字及背景(源代码分享)

使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream.ledong; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gr

Android自定义DataTimePicker(日期选择器)

Android自定义DataTimePicker(日期选择器) Android日期时间选择器实现以及自定义大小

Android基于wheelView的自定义日期选择器(可拓展样式)

基于wheelView的自定义日期选择器 项目要求效果图: 要求 "6月20 星期五" 这一项作为一个整体可以滑动,"7时"."48分"分别作为一个滑动整体. 系统自带的DatePicker.TimePicker大家都知道,只有这种效果: 百度了很多,试了NumberPicker等都不行,本来打算自己写.网友推荐了一个开源组件WheelView,下下来试了试,发现他已经定义的很完善了,在他的基础上拓展很容易. 现将基于wheelView自定义日期

android自定义listview实现圆角

在项目中我们会经常遇到这种圆角效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样既会是自己的项目增大也会增加内存使用量,所以使用shape来实现不失为一种更好的实现方式.在这里先看一下shape的使用: [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schema

Android自定义TabActivity(实现仿新浪微博底部菜单更新UI)

如今Android上很多应用都采用底部菜单控制更新的UI这种框架,例如新浪微博 点击底部菜单的选项可以更新界面.底部菜单可以使用TabHost来实现,不过用过TabHost的人都知道自定义TabHost究竟是有多麻烦的,原生TabHost的风格是不依附屏幕的底部的,要依附底部就要重写布局. TabHost设置的Container可以管理UI的显示,UI可以用LayoutInflater动态生成,也可以是Activity,但不好管理Activity的生命周期.然后用TabHost控制显示UI的显示

android--解决方案--自定义tabhost(动态添加选项+带自动水平滑动选项卡+手势切换选项卡及内容功能)

本文主要解决自定义tabhost的实现,以及集成通过代码动态添加选项卡功能.选项卡水平自动滑动功能.以及通过手势来切换选项卡功能. 下面跟我一起来完成这个完美的解决方案: 1.定义tabwidget选项卡的布局:tab_button.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/r

Android底部TabHost API

今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 1 package com.api; 2 3 import android.app.TabActivity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.LayoutInfla