单选按钮模拟QQ app的Fragment

这篇博客介绍了如何用单选按钮替代传统按钮,在Android应用中创建类似QQ App的Fragment效果。作者通过自定义MyFragmentActivity继承FragmentActivity来监听按钮状态变化,并更新图片。在实现过程中,注意使用support.v4包以避免错误,样式配置存储于styles.xml文件,通过java代码实现按钮切换的监听逻辑。

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

Fragment以前学过,这一次使用单选按钮来替换原来的按钮,并且加上了图片,更改了样式。

效果:

    




   

    写一个自己的MyFragmentActivity去继承FragmentActivity,实现监听,监听底下三个按钮的变换,然后更换图片。

    使用Fragment要使用support.v4包,使用其他包发生过错误情况。

    这里写的配置文件可以作为一种统一的样式抽取出来,放在styles.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" >

	<FrameLayout
	    android:id="@+id/flContent"
	    android:layout_width="match_parent"
        android:layout_height="match_parent"
	    android:layout_weight="1"
	    ></FrameLayout>
    
	<RadioGroup
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
	    >
	    <RadioButton
	        android:id="@+id/rgMsg"
	        android:text="消息"
	        style="@style/mainRbStyle"
	        android:drawableTop="@drawable/main_btn1_selector"
	        />
	    <RadioButton
	        android:id="@+id/rgContact"
	        android:text="联系人"
	        style="@style/mainRbStyle"
	        android:drawableTop="@drawable/main_btn2selector"
	        />
	    <RadioButton
	         android:id="@+id/rgDo"
	        android:text="动态"
	        style="@style/mainRbStyle"
	        android:drawableTop="@drawable/main_btn3selector"
	        />
	</RadioGroup>
</LinearLayout>

    

styles.xml文件

    

 <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
	 <style name="mainRbStyle" parent="android:Widget.Holo.Light.CompoundButton.RadioButton">
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">18sp</item>
        
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        
        <item name="android:textColor">@color/main_btn_color_selector</item>
    </style>

然后按钮添加图片和selector.xml配置文件来配置自己的按钮样式

<span style="font-size:24px;"><selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/icon_sw_selected" />
    <item android:state_checked="false" android:drawable="@drawable/icon_sw" />

</selector></span>


最后就是java代码

    

添加转换按钮监听

	public void onCheckedChanged(CompoundButton buttonView, boolean arg1) {
		int id = buttonView.getId();
		switch (id) {
		case R.id.rgMsg:
			if (mRgMsg.isChecked())
				break;
		case R.id.rgContact:
			if (mRgContact.isChecked())
				break;
		case R.id.rgDo:
			if (mRgDo.isChecked())
				break;
		default:
			break;
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值