仿主页效果TabHost与RadioGroup应用

来源:互联网 发布:fedora25 yum 配置源 编辑:程序博客网 时间:2024/05/22 08:43

运行效果图



新建布局.xml,以及相应的.java


例如:activity_one.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#5c3317"    android:gravity="top|center"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是好友的activity" /></LinearLayout>

OneActivity.java文件

import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class OneActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_one);}<span style="white-space:pre"></span>//只继承}<strong><span style="font-size:18px;"></span></strong>

activity_one.xml,activity_two.xml,activity_three.xml,activity_four.xml,activity_five.xml对应的文件
OneActivity.java,TwoActivity.java,ThreeActivity.java,FourActivity.java,FiveActivity.java,都如上相同只不过内容自定义不同

activity_main.xml修改如下:

提前在values/style文件下添加下列代码:
<!-- 去除前面的多选框 -->    <style name="style">    <item name="android:button">@null</item>    </style>
然后再修改主布局文件
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@android:id/tabhost"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <LinearLayout         android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical">        <FrameLayout             android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="0.0dip"            android:layout_weight="1.0"/>        <TabWidget             android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="0.0"            android:visibility="gone"/>        <RadioGroup            android:id="@+id/main_radio"            android:layout_width="match_parent"            android:layout_height="50sp"            android:layout_gravity="bottom"            android:background="@drawable/bg"            android:gravity="center_vertical"            android:orientation="horizontal" >            <RadioButton                 android:id="@+id/rbtn_1"                android:layout_height="match_parent"                android:layout_width="match_parent"                android:tag="rbtn_1"                android:layout_marginTop="2.0dip"                android:drawableTop="@drawable/bg_1"                style="@style/style"                android:layout_weight="1.0"                android:gravity="center"                android:text="好友"/>           <RadioButton               android:id="@+id/rbtn_2"               android:layout_height="match_parent"                android:layout_width="match_parent"               style="@style/style"               android:layout_marginTop="2.0dip"               android:drawableTop="@drawable/bg_2"               android:tag="rbtn_2"               android:layout_weight="1.0"               android:gravity="center"               android:text="广场" />           <RadioButton               android:id="@+id/rbtn_3"               android:layout_height="match_parent"                android:layout_width="match_parent"               style="@style/style"               android:layout_marginTop="2.0dip"               android:drawableTop="@drawable/bg_3"               android:layout_weight="1.0"               android:tag="rbtn_3"               android:gravity="center"               android:text="首页" />           <RadioButton               android:id="@+id/rbtn_4"               android:layout_height="match_parent"                android:layout_width="match_parent"               style="@style/style"               android:layout_marginTop="2.0dip"               android:drawableTop="@drawable/bg_4"               android:tag="rbtn_4"               android:layout_weight="1.0"               android:gravity="center"               android:text="消息" />           <RadioButton               android:id="@+id/rbtn_5"               android:layout_height="match_parent"                android:layout_width="match_parent"               style="@style/style"               android:layout_marginTop="2.0dip"               android:drawableTop="@drawable/bg_5"               android:tag="rbtn_5"               android:layout_weight="1.0"               android:gravity="center"               android:text="设置" />                    </RadioGroup>    </LinearLayout></TabHost>


修改MainActivity.java文件中的代码:
import android.os.Bundle;import android.app.Activity;import android.app.TabActivity;import android.content.Intent;import android.view.Menu;import android.widget.RadioGroup;import android.widget.TabHost;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends TabActivity implements OnCheckedChangeListener {private TabHost tabHost;//定义TabHost对象private RadioGroup radioderGroup;//定义RadioGroup对象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);settabHost();}private void settabHost() {tabHost=this.getTabHost();//实例化TabHost//添加选项卡并且跳转IntenttabHost.addTab(tabHost.newTabSpec("tab_1").setIndicator("好友").setContent(new Intent(this,OneActivity.class)));tabHost.addTab(tabHost.newTabSpec("tab_2").setIndicator("广场").setContent(new Intent(this,TwoActivity.class)));tabHost.addTab(tabHost.newTabSpec("tab_3").setIndicator("首页").setContent(new Intent(this,ThreeActivity.class)));tabHost.addTab(tabHost.newTabSpec("tab_4").setIndicator("消息").setContent(new Intent(this,FourActivity.class)));tabHost.addTab(tabHost.newTabSpec("tab_5").setIndicator("设置").setContent(new Intent(this,FiveActivity.class)));//得到RadioGroup对象radioderGroup=(RadioGroup) findViewById(R.id.main_radio);//设置RadioGroup对象的切换监听器radioderGroup.setOnCheckedChangeListener(this);}//事项OnCheckedChangeListener中的RadioGroup的选项切换回调函数@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {//根据所选中的RadioGroup的选项id,设置tabHost的选项卡switch (checkedId) {case R.id.rbtn_1:tabHost.setCurrentTabByTag("tab_1");break;case R.id.rbtn_2:tabHost.setCurrentTabByTag("tab_2");break;case R.id.rbtn_3:tabHost.setCurrentTabByTag("tab_3");break;case R.id.rbtn_4:tabHost.setCurrentTabByTag("tab_4");break;case R.id.rbtn_5:tabHost.setCurrentTabByTag("tab_5");break;}}}

之后运行即可,可以扩展功能哦!







0 0
原创粉丝点击