快捷搜索:

Android进阶:学习新浪微博自定义radioButton样式

Android带的那个RadioButton样子真是丢脸了 有木有啊

一样平常我们用的时刻这个要根据整体的界面风格来定义,看着才顺眼,新浪微博里面有个这样的自定义样式,进修了下

来看效果:

轻细看着顺眼点了吧

来看实现,首先结构文件

xml version="1.0" encoding="utf-8"?>

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"

android:layout_width="fill_parent"android:layout_height="fill_parent"

android:background="@drawable/search_bg">

RadioGroup android:gravity="center"android:layout_gravity="bottom"

android:orientation="horizontal"android:id="@+id/searchRadioGroup"

android:layout_width="fill_parent"android:layout_height="wrap_content" >

RadioButton android:textSize="17.0sp"android:id="@+id/searchRadioShare"

android:tag="radio_button0"android:text="搜索微博"

android:layout_weight="1"style="@style/CustomLeftRadioBtn">

RadioButton>RadioButton android:textSize="17.0sp"

android:id="@+id/searchRadioUser"android:tag="radio_button1"

android:text="搜索用户"android:layout_weight="1"

style="@style/CustomRightRadioBtn">RadioButton>

RadioGroup> LinearLayout>

在RadioGroup中定义RadioButton并设置style="@style/CustomLeftRadioBtn"

定义如下

xml version="1.0" encoding="utf-8"?>

resources>mce:style name="CustomLeftRadioBtn">

item name="android:button">@nullitem>item name="android:drawableLeft">@drawable/search_radio_1item>

item name="android:background">@drawable/search_leftitem>item name="android:gravity">center_verticalitem>

-->mce:style>style name="CustomLeftRadioBtn" mce_bogus="1">item name="android:button">@nullitem>item name="android:drawableLeft">@drawable/search_radio_1item>

item name="android:background">@drawable/search_leftitem>item name="android:gravity">center_verticalitem>

style>

mce:style name="CustomRightRadioBtn">item name="android:button">@nullitem>

item name="android:drawableLeft">@drawable/search_radio_1item>item name="android:background">@drawable/search_rightitem>

item name="android:gravity">center_verticalitem>

-->mce:style>style name="CustomRightRadioBtn" mce_bogus="1">item name="android:button">@nullitem>item name="android:drawableLeft">@drawable/search_radio_1item>

item name="android:background">@drawable/search_rightitem>item name="android:gravity">center_verticalitem>

style> resources>

然后添加Onclick事故就可以了

import android.app.Activity;

import android.os.Bundle;import android.view.View;

import android.view.View.OnClickListener;import android.widget.Button;

import android.widget.Toast;public class RadioButtonDemo extends Activity {

/** Called when the activity is first created. */@Override

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button searchRadioShare = (Button) findViewById(R.id.searchRadioShare);Button searchRadioUser = (Button) findViewById(R.id.searchRadioUser);

searchRadioShare.setOnClickListener(l);

searchRadioUser.setOnClickListener(l);}

private View.OnClickListener l = new OnClickListener(){

@Overridepublic void onClick(View v) {

if(v.getId()==R.id.searchRadioShare){Toast.makeText(RadioButtonDemo.this, "搜索微博", Toast.LENGTH_LONG).show();

}else{Toast.makeText(RadioButtonDemo.this, "搜索用户", Toast.LENGTH_LONG).show();

}

}

};}

可以下载项目:RadioButtonDemo

您可能还会对下面的文章感兴趣: