您好,登錄后才能下訂單哦!
Android中TabHost如何使用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
<?xml version="1.0" encoding="UTF-8"?> <!-- Tab導航 最新版 --> <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" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF" > </FrameLayout> <!-- TabWidget管理所有的選項卡,id名是android指定的 --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" /> <!-- Frame下放置單選群組替代TAB效果 --> <RadioGroup android:id="@+id/main_radio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ccc" android:gravity="center_vertical" android:orientation="horizontal" android:layout_alignParentBottom="true" > <RadioButton android:id="@+id/tab_icon_qq" android:checked="true" android:drawableTop="@drawable/message" android:text="消息" /> <RadioButton android:id="@+id/tab_icon_address" android:checked="false" android:drawableTop="@drawable/home" android:text="班級" /> <RadioButton android:id="@+id/tab_icon_myself" android:drawableTop="@drawable/d1" android:checked="false" android:text="我" /> </RadioGroup> </RelativeLayout> </TabHost>
MainActivity
public class MainActivity extends TabActivity { public static TabHost mTabHost; public static TabHost getmTabHost() { return mTabHost; } private RadioGroup main_radio; private RadioButton tab_icon_qq, tab_icon_address,tab_icon_myself; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = getTabHost(); final TabWidget tabWidget = mTabHost.getTabWidget(); tabWidget.setStripEnabled(false); mTabHost.addTab(mTabHost.newTabSpec("TAG1").setIndicator("0").setContent(new Intent(this,MessageActivity.class))); mTabHost.addTab(mTabHost.newTabSpec("TAG2").setIndicator("1").setContent(new Intent(this,ClassActivity.class))); mTabHost.addTab(mTabHost.newTabSpec("TAG3").setIndicator("2").setContent(new Intent(this,MyselfActivity.class))); main_radio = (RadioGroup) findViewById(R.id.main_radio); tab_icon_weixin = (RadioButton) findViewById(R.id.tab_icon_weixin); tab_icon_address = (RadioButton) findViewById(R.id.tab_icon_address); tab_icon_myself = (RadioButton) findViewById(R.id.tab_icon_myself); main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int id) { if (id == tab_icon_weixin.getId()) { mTabHost.setCurrentTab(0); } else if (id == tab_icon_address.getId()) { mTabHost.setCurrentTab(1); } else if (id == tab_icon_myself.getId()) { mTabHost.setCurrentTab(2); } } }); // 設置當前顯示哪一個標簽 mTabHost.setCurrentTab(0); // 遍歷tabWidget每個標簽, for (int i = 0; i < tabWidget.getChildCount(); i++) { View vv = tabWidget.getChildAt(i); vv.getLayoutParams().height = 45; // vv.getLayoutParams().width = 65; vv.setBackgroundColor(Color.BLUE); } // findViewById(R.id.tab_icon_brand).setOnClickListener(this); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。