Android仿新浪微博分頁管理介面(3)_Android

來源:互聯網
上載者:User

本文執行個體為大家分享了Android仿新浪微博分頁管理介面的具體代碼,供大家參考,具體內容如下

多個activity分頁管理,為了方便擷取上下文,採用繼承TabActivity的傳統方法。

大致思路:使用RadioGroup點擊觸發不同的選卡項,選卡項綁定不同的activiity,進而進行分頁管理。詳解見註解。

/** * 主Activity * 通過點擊RadioGroup下的RadioButton來切換不同介面 * Created by D&LL on 2016/7/20. */public class MainActivity extends TabActivity { //定義TabHost對象 private TabHost tabHost; //定義RadioGroup對象 private RadioGroup radioGroup; public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.tab_layout);  initView();  initData(); } /**  * 初始化組件  */ private void initView() {  //執行個體化TabHost,得到TabHost對象  tabHost = getTabHost();  //得到Activity的個數  int count = Constant.ConValue.mTabClassArray.length;  for (int i = 0; i < count; i++) {   //為每一個Tab按鈕設定表徵圖、文字和內容   TabSpec tabSpec = tabHost.newTabSpec(Constant.ConValue.mTextviewArray[i])     .setIndicator(Constant.ConValue.mTextviewArray[i]).setContent(getTabItemIntent(i));   //將Tab按鈕添加進Tab選項卡中   tabHost.addTab(tabSpec);  }  //執行個體化RadioGroup  radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup); } /**  * 初始化組件  */ private void initData() {  // 給radioGroup設定監聽事件  radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {   public void onCheckedChanged(RadioGroup group, int checkedId) {    switch (checkedId) {     case R.id.RadioButton0:      tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[0]);      break;     case R.id.RadioButton1:      tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[1]);      break;     case R.id.RadioButton2:      tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[2]);      break;     case R.id.RadioButton3:      tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[3]);      break;     case R.id.RadioButton4:      tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[4]);      break;    }   }  });  ((RadioButton) radioGroup.getChildAt(0)).toggle(); } /**  * 給Tab選項卡設定內容(每個內容都是一個Activity)  */ private Intent getTabItemIntent(int index) {  Intent intent = new Intent(this, Constant.ConValue.mTabClassArray[index]);  return intent; }}

MainActivity布局檔案tab_layout.xml. TabHost布局,添加一個TabWidget用於顯示activity,下面是一個RadioGroup顯示切換activity的按鈕菜單。

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"   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_radiogroup"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:layout_gravity="bottom"   android:background="@drawable/tab_widget_background"   android:gravity="center_vertical"   android:orientation="horizontal"   android:padding="2dip">   <RadioButton    android:id="@+id/RadioButton0"    style="@style/tab_item_background"    android:drawableTop="@drawable/tab_home"    android:text="首頁"    android:textColor="#ffffff"/>   <RadioButton    android:id="@+id/RadioButton1"    style="@style/tab_item_background"    android:drawableTop="@drawable/tab_msg"    android:text="評論"    android:textColor="#ffffff"/>   <RadioButton    android:id="@+id/RadioButton2"    style="@style/tab_item_background"    android:drawableTop="@drawable/tab_write"    android:text="發微博"    android:textColor="#ffffff"/>   <RadioButton    android:id="@+id/RadioButton3"    style="@style/tab_item_background"    android:drawableTop="@drawable/tab_me"    android:text="使用者資訊"    android:textColor="#ffffff"/>   <RadioButton    android:id="@+id/RadioButton4"    style="@style/tab_item_background"    android:drawableTop="@drawable/tab_more"    android:text="更多"    android:textColor="#ffffff"/>  </RadioGroup> </LinearLayout></TabHost>

效果圖:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.