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;
}
}