{"id":846,"date":"2015-07-03T16:54:42","date_gmt":"2015-07-03T08:54:42","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=846"},"modified":"2015-07-03T17:15:24","modified_gmt":"2015-07-03T09:15:24","slug":"android%e4%b8%adtabhost%e4%b8%bb%e7%95%8c%e9%9d%a2%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=846","title":{"rendered":"Android\u4e2dTabHost\u4e3b\u754c\u9762\u5b9e\u73b0"},"content":{"rendered":"<p>\u5728App\u8bbe\u8ba1\u4e2d\u901a\u5e38\u4f1a\u8bbe\u8ba1\u4e00\u4e2a\u4e3b\u754c\u9762\u4f5c\u4e3a\u8fdb\u5165\u5176\u5b83\u754c\u9762\u7684\u5165\u53e3,\u4e00\u822c\u4f1a\u9009\u62e9\u9009\u9879\u5361\u4e5f\u53eb\u9875\u5361Tab\u6765\u5b9e\u73b0.<br \/>\n\u4eca\u5929\u6211\u4eec\u6765\u63a2\u8ba8\u4e0bAndroid\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e00\u4e2a\u53e4\u8001\u7684\u5b9e\u73b0\u65b9\u5f0f:TabActivity+TabHost, \u540e\u9762\u5728\u5206\u6790FramgentActivity\u5b9e\u73b0\u65b9\u5f0f.<br \/>\n<!--more--><\/p>\n<p><strong>\u9996\u5148\u6211\u4eec\u6765\u770b\u4e0bTab\u9009\u9879\u5361\u7684\u771f\u9762\u76ee.<\/strong><br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/anzhi1.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-847\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/anzhi1.gif\" alt=\"anzhi1\" width=\"273\" height=\"433\" \/><\/a><\/p>\n<p>\u6211\u4eec\u770b\u5230\u662f\u4e00\u4e2aAPP\u7684\u4e3b\u754c\u9762\uff0cTAB\u9875\u5361\u4f4d\u4e8e\u5c4f\u5e55\u5e95\u90e8\uff0c\u6211\u4eec\u70b9\u51fb\u9875\u5361\u540e\u5207\u6362\u5230\u4e0d\u540c\u754c\u9762.<\/p>\n<p>\u4e3b\u5c4f\u5e55\u88ab\u5206\u6210\u4e86\u4e09\u4e2a\u533a\u57df:\u6807\u9898\u533a\u57df\uff0c\u9875\u5361\u5185\u5bb9\u533a\u57df\uff0c\u9875\u5361\u533a\u57df.<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150703163230.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-848\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150703163230.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150703163230\" width=\"387\" height=\"385\" \/><\/a><\/p>\n<p><strong>\u5206\u6790\u5b8c\u754c\u9762\u6784\u9020\u540e\u63a5\u7740\u6211\u4eec\u770b\u4e0b\u5177\u4f53\u5982\u4f55\u5b9e\u73b0<\/strong><br \/>\n1.\u5e03\u5c40\u6587\u4ef6<\/p>\n<p>[java]  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;TabHost xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n    android:id=&quot;@android:id\/tabhost&quot;<br \/>\n    android:layout_width=&quot;match_parent&quot;<br \/>\n    android:layout_height=&quot;match_parent&quot; &gt;<\/p>\n<p>    &lt;RelativeLayout<br \/>\n        android:layout_width=&quot;match_parent&quot;<br \/>\n        android:layout_height=&quot;match_parent&quot; &gt;<\/p>\n<p>        &lt;TabWidget<br \/>\n            android:id=&quot;@android:id\/tabs&quot;<br \/>\n            android:layout_width=&quot;match_parent&quot;<br \/>\n            android:layout_height=&quot;wrap_content&quot;<br \/>\n            android:layout_alignParentBottom=&quot;true&quot;<br \/>\n            android:background=&quot;@android:color\/white&quot; &gt;<br \/>\n        &lt;\/TabWidget&gt;<\/p>\n<p>        &lt;FrameLayout<br \/>\n            android:id=&quot;@android:id\/tabcontent&quot;<br \/>\n            android:layout_width=&quot;match_parent&quot;<br \/>\n            android:layout_height=&quot;match_parent&quot;<br \/>\n            android:layout_above=&quot;@android:id\/tabs&quot; &gt;<br \/>\n        &lt;\/FrameLayout&gt;<br \/>\n    &lt;\/RelativeLayout&gt;<\/p>\n<p>&lt;\/TabHost&gt;[\/java]<\/p>\n<p>\u5176\u4e2dTabWidget\u8868\u793a\u9875\u5361\u533a\u57df,FrameLayout\u5e03\u5c40\u4e3a\u9875\u5361\u5185\u5bb9\u533a\u57df.<\/p>\n<p>2.\u4ee3\u7801<\/p>\n<p>[java]@Override<br \/>\n\tprotected void onCreate(Bundle savedInstanceState) {<br \/>\n\t\tsuper.onCreate(savedInstanceState);<\/p>\n<p>\t\tsetContentView(R.layout.activity_tab1_layout);<\/p>\n<p>\t\tTabHost tabHost = getTabHost();<\/p>\n<p>\t\tTabSpec tabSpec1 = tabHost.newTabSpec(&quot;tab1&quot;);<br \/>\n\t\ttabSpec1.setIndicator(&quot;\u9875\u53611&quot;);<br \/>\n\t\ttabSpec1.setContent(new Intent(context, RadioButtonActivity.class));<\/p>\n<p>\t\tTabSpec tabSpec2 = tabHost.newTabSpec(&quot;tab2&quot;);<br \/>\n\t\ttabSpec2.setIndicator(&quot;\u9875\u53612&quot;);<br \/>\n\t\ttabSpec2.setContent(new Intent(context, LinerLayoutAcitiy.class));<\/p>\n<p>\t\tTabSpec tabSpec3 = tabHost.newTabSpec(&quot;tab3&quot;);<br \/>\n\t\ttabSpec3.setIndicator(&quot;\u9875\u53613&quot;);<br \/>\n\t\ttabSpec3.setContent(new Intent(context, SimpleListActivity.class));<\/p>\n<p>\t\ttabHost.addTab(tabSpec1);<br \/>\n\t\ttabHost.addTab(tabSpec2);<br \/>\n\t\ttabHost.addTab(tabSpec3);<\/p>\n<p>\t}<br \/>\n[\/java]<\/p>\n<p>\u7ecf\u8fc7\u4e0a\u8ff0\u64cd\u4f5c\u5b9e\u73b0\u6548\u679c\u4e3a\u7cfb\u7edf\u6548\u679c,\u4e3a\u4e86\u5f97\u5230\u66f4\u597d\u770b\u7684\u9875\u5361\uff0c\u9700\u8981\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a\u9875\u5361View,\u901a\u8fc7setIndicator(View view)\u65b9\u6cd5\u8bbe\u7f6e.<\/p>\n<p><strong>\u81ea\u5b9a\u4e49View\u65b9\u6cd5<\/strong><\/p>\n<p>[java]private View createIndicatorView(int tab) {<br \/>\n\t\tView view = mLayoutInflater.inflate(R.layout.view_tab_item_layout, null);<\/p>\n<p>\t\tTextView titleTxt = (TextView) view.findViewById(R.id.tab_title);<br \/>\n\t\tImageView iconImg = (ImageView) view.findViewById(R.id.tab_icon);<\/p>\n<p>\t\tswitch(tab){<br \/>\n\t\tcase HOME_TAB:<br \/>\n\t\t\ttitleTxt.setText(&quot;\u9996\u9875&quot;);<br \/>\n\t\t\ticonImg.setImageResource(R.drawable.selector_drawable_home_tab);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase CONTENT_TAB:<br \/>\n\t\t\ttitleTxt.setText(&quot;\u5546\u5bb6&quot;);<br \/>\n\t\t\ticonImg.setImageResource(R.drawable.selector_drawable_content_tab);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MORE_TAB:<br \/>\n\t\t\ttitleTxt.setText(&quot;\u66f4\u591a&quot;);<br \/>\n\t\t\ticonImg.setImageResource(R.drawable.selector_drawable_set_tab);<br \/>\n\t\t\tbreak;<br \/>\n\t\t}<\/p>\n<p>\t\treturn view;<br \/>\n\t}[\/java]<\/p>\n<p>\u51c6\u5907\u76f8\u5173\u56fe\u7247\u8d44\u6e90,\u9875\u5361\u56fe\u6807selector\u6587\u4ef6\u5b9a\u4e49:<\/p>\n<p>[java]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;selector xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;&gt;<\/p>\n<p>    &lt;item android:drawable=&quot;@drawable\/ic_menu_more_on&quot; android:state_pressed=&quot;true&quot;&gt;&lt;\/item&gt;<br \/>\n    &lt;item android:drawable=&quot;@drawable\/ic_menu_more_on&quot; android:state_selected=&quot;true&quot;&gt;&lt;\/item&gt;<br \/>\n    &lt;item android:drawable=&quot;@drawable\/ic_menu_more_off&quot;&gt;&lt;\/item&gt;<\/p>\n<p>&lt;\/selector&gt;<br \/>\n[\/java]<\/p>\n<p>\u9875\u5361\u6587\u5b57selector\u6587\u4ef6\u5b9a\u4e49:<\/p>\n<p>[java]&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;selector xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;&gt;<\/p>\n<p>    &lt;item android:state_pressed=&quot;true&quot; android:color=&quot;@color\/green_tab&quot;&gt;&lt;\/item&gt;<br \/>\n    &lt;item android:state_selected=&quot;true&quot; android:color=&quot;@color\/green_tab&quot;&gt;&lt;\/item&gt;<br \/>\n    &lt;item android:color=&quot;@color\/darker_gray_tab&quot;&gt;&lt;\/item&gt;<\/p>\n<p>&lt;\/selector&gt;[\/java]<\/p>\n<p>\u9875\u5361View\u5e03\u5c40\u6587\u4ef6\u5b9a\u4e49<\/p>\n<p>[java]&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n    android:layout_width=&quot;match_parent&quot;<br \/>\n    android:layout_height=&quot;match_parent&quot;<br \/>\n    android:orientation=&quot;vertical&quot;<br \/>\n    android:gravity=&quot;center&quot;<br \/>\n    android:paddingTop=&quot;3dp&quot;<br \/>\n    android:paddingBottom=&quot;3dp&quot;&gt;<\/p>\n<p>    &lt;TextView<br \/>\n        android:id=&quot;@+id\/tab_title&quot;<br \/>\n        android:layout_width=&quot;wrap_content&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:textColor=&quot;@drawable\/selector_color_home_tab&quot;<br \/>\n        android:text=&quot;\u9996\u9875&quot; \/&gt;<\/p>\n<p>    &lt;ImageView<br \/>\n        android:id=&quot;@+id\/tab_icon&quot;<br \/>\n        android:layout_width=&quot;wrap_content&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:src=&quot;@drawable\/selector_drawable_home_tab&quot;\/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;[\/java]<\/p>\n<p><strong>\u5b9e\u73b0\u6548\u679c<\/strong><\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/tabhost1.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-849\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/tabhost1.gif\" alt=\"tabhost1\" width=\"290\" height=\"468\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728App\u8bbe\u8ba1\u4e2d\u901a\u5e38\u4f1a\u8bbe\u8ba1\u4e00\u4e2a\u4e3b\u754c\u9762\u4f5c\u4e3a\u8fdb\u5165\u5176\u5b83\u754c\u9762\u7684\u5165\u53e3,\u4e00\u822c\u4f1a\u9009\u62e9\u9009\u9879\u5361\u4e5f\u53eb\u9875\u5361Tab\u6765\u5b9e\u73b0. \u4eca\u5929\u6211\u4eec\u6765\u63a2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/846"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=846"}],"version-history":[{"count":6,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/846\/revisions"}],"predecessor-version":[{"id":855,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/846\/revisions\/855"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}