[Android学UI之四]实现分断Button,模仿MIUI设置页面顶部Button

这篇博客介绍了如何在Android中实现分段Button,模仿MIUI设置页面顶部的样式。通过使用RidaoGroup包裹RidaoButton来拼接,提供了XML布局和自定义View的代码示例,展示了简单的实现步骤。同时,博客作者分享了四个用于按压效果的drawable资源文件,并邀请读者分享更好的实现方式,共同学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


功能:

    拼接的Button。


使用说明:

    用RidaoGroup包裹几个RidaoButton,实现拼接。


还是看图,更真实!!!



页面做的比较简单,这个功能也不太难。。这只是其中的实现方式之一。有其它更好的方式,请告之。


下面还是看代码吧:

界面Activity:

package com.bbswp.topbuttondemo;

import com.bbswp.topbuttondemo.view.SegmentedRadioGroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SegmentedRadioGroup group = (SegmentedRadioGroup) findViewById(R.id.segment_text);
        group.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.button_one:
                toast(1);
                break;

            case R.id.button_two:
                toast(2);
                break;

            case R.id.button_three:
                toast(3);
                break;

            default:
                break;
        }

    }

    public void onClick(View view) {
        toast(4);
    }

    private void toast(int id) {
        Toast.makeText(this, "点击:" + id, 0).show();
    }

}



自定义一个View,用于包裹RidaoButton.


看代码:

/*
 * Copyright (C) 2011 Make Ramen, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bbswp.topbuttondemo.view;

import com.bbswp.topbuttondemo.R;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RadioGroup;

public class SegmentedRadioGroup extends RadioGroup {

	public SegmentedRadioGroup(Context context) {
		super(context);
	}

	public SegmentedRadioGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		changeButtonsImages();
	}

	private void changeButtonsImages(){
		int count = super.getChildCount();

		if(count > 1){
			super.getChildAt(0).setBackgroundResource(R.drawable.segment_radio_left);
			for(int i=1; i < count-1; i++){
				super.getChildAt(i).setBackgroundResource(R.drawable.segment_radio_mid);
			}
			super.getChildAt(count-1).setBackgroundResource(R.drawable.segment_radio_right);
		}else if (count == 1){
			super.getChildAt(0).setBackgroundResource(R.drawable.segment_radio_button);
		}
	}
}



界面少不了xml。。。看看吧:

<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="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <com.bbswp.topbuttondemo.view.SegmentedRadioGroup
        android:id="@+id/segment_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:checkedButton="@+id/button_one"
        android:gravity="top|center"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@id/button_one"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="我是第一个"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <RadioButton
            android:id="@+id/button_two"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="我是第二个"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <RadioButton
            android:id="@+id/button_three"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="我是第三个"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </com.bbswp.topbuttondemo.view.SegmentedRadioGroup>

    <com.bbswp.topbuttondemo.view.SegmentedRadioGroup
        android:id="@+id/segment_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:checkedButton="@+id/button_one"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/button_four"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:onClick="onClick"
            android:text="我是单独的一个"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </com.bbswp.topbuttondemo.view.SegmentedRadioGroup>

</LinearLayout>


这里还有重要的四个drawable文件,用于按下的效果显示:

segment_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"  android:drawable="@drawable/segment_grey" />
    <item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_grey_focus" />
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_white_focus" />
    
    <item android:state_pressed="true"  android:drawable="@drawable/segment_grey_press" /> 
    <item android:state_checked="false"  android:drawable="@drawable/segment_white" />
    <item android:drawable="@drawable/segment_grey" />  <!-- default  -->
</selector>


后面是左:

segment_radio_left.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/segment_radio_grey_left_focus" android:state_checked="true" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/segment_radio_white_left_focus" android:state_checked="false" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/segment_radio_grey_left_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/segment_radio_white_left" android:state_checked="false"/>
    <item android:drawable="@drawable/segment_radio_grey_left"/>

</selector>


中:

segment_radio_mid.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_radio_grey_middle_focus" />
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_radio_white_middle_focus" />
    
    <item android:state_pressed="true"  android:drawable="@drawable/segment_radio_grey_middle_press" /> 
    <item android:state_checked="false" android:drawable="@drawable/segment_radio_white_middle" /> 
    <item android:drawable="@drawable/segment_radio_grey_middle" /> 
</selector>


右:

segment_radio_right.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_radio_grey_right_focus" />
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_radio_white_right_focus" />
    
    <item android:state_pressed="true"  android:drawable="@drawable/segment_radio_grey_right_press" /> 
    <item android:state_checked="false" android:drawable="@drawable/segment_radio_white_right" />   
    <item android:drawable="@drawable/segment_radio_grey_right" />

</selector>


用于学习。。还是可以吧。。。


有更好的方式,一起交流吧!!!



代码当然要分享给大家了,一起学习!!!

下载:http://download.youkuaiyun.com/detail/hudan2714/4814643




评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值