Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的時候遇到了一些奇怪的問題,在這里總結(jié)分享備忘一下。
首先說一點TabActivity將會被FragmentActivity所替代,但是本文中卻是使用的TabActivity。
下面說說本程序能夠?qū)崿F(xiàn)的功能:
下面分幾步來分別實現(xiàn)以上的功能:
第一步,先實現(xiàn)一個基本的TabHost的展現(xiàn)
詳細的說明可以在網(wǎng)上其它地方搜的,主要就是注意一點,控件的id的是固定的不能隨便更改,并且@和id之間不能加+;
Activity的代碼如下:
public class TabhostTestActivity extends TabActivity implementsTabContentFactory {private final String[] tabTitle = { "測試Tab標簽1", "測試Tab標簽2", "測試Tab標簽3","測試Tab標簽4", "測試Tab標簽5", "測試Tab標簽6", "測試Tab標簽7", "測試Tab標簽8","測試Tab標簽9" };/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabhost);TabHost th = getTabHost();for (int i = 0; i < tabTitle.length; i++) {TextView view = (TextView) getLayoutInflater().inflate(R.layout.tabwighet, null);view.setText(tabTitle[i]);th.addTab(th.newTabSpec(tabTitle[i]).setIndicator(view).setContent(this));}}@Overridepublic View createTabContent(String tag) {View view = new View(this);if (tabTitle[0].equals(tag)) {view.setBackgroundColor(Color.BLUE);} else if (tabTitle[1].equals(tag)) {view.setBackgroundColor(Color.CYAN);} else if (tabTitle[2].equals(tag)) {view.setBackgroundColor(Color.DKGRAY);} else if (tabTitle[3].equals(tag)) {view.setBackgroundColor(Color.GRAY);} else if (tabTitle[4].equals(tag)) {view.setBackgroundColor(Color.GREEN);} else if (tabTitle[5].equals(tag)) {view.setBackgroundColor(Color.LTGRAY);} else if (tabTitle[6].equals(tag)) {view.setBackgroundColor(Color.MAGENTA);} else if (tabTitle[7].equals(tag)) {view.setBackgroundColor(Color.RED);} else if (tabTitle[8].equals(tag)) {view.setBackgroundColor(Color.YELLOW);}return view;} }對應(yīng)的layout的xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="wrap_content"android:layout_height="wrap_content" ></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ></FrameLayout></LinearLayout></TabHost></LinearLayout> <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:singleLine="true" />運行后的效果圖如下(平板1280*800的設(shè)備):
第二步,給標簽欄添加橫向的滾動操作
這個很簡單,只需要修改一下tabhost.xml即可:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><HorizontalScrollViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:fillViewport="true"android:scrollbars="none" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="wrap_content"android:layout_height="wrap_content" ></TabWidget></HorizontalScrollView><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ></FrameLayout></LinearLayout></TabHost></LinearLayout>運行效果如下:
第三步,修改標簽的寬度和高度
這里要改2個地方,一個是activity中,一個是tabwighet.xml中;
首先說明一下tabwighet.xml修改,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:gravity="center_horizontal"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_title"android:layout_width="200dp"android:layout_height="50dp"android:layout_gravity="center"android:gravity="center"android:singleLine="true" /></LinearLayout>其中需要注意一點,設(shè)置標簽的寬度和高度不能設(shè)置到LinearLayout上,否則設(shè)置的寬度和高度不起作用。
Activity的修改就很簡單了,根據(jù)tabwighet.xml的修改稍微調(diào)整一下代碼即可,修改的代碼如下:
LinearLayout view = (LinearLayout) getLayoutInflater().inflate( R.layout.tabwighet, null); ((TextView) view.findViewById(R.id.tv_title)).setText(tabTitle[i]);運行的效果如下:
第四步,修改標簽的樣式,增加背景圖片和選中的狀態(tài),以及選中后的字體的顏色
?首先修改tabhost.xml文件,為HorizontalScrollView和FrameLayout添加設(shè)置background的屬性,圖片自己選個即可。代碼如下:
<HorizontalScrollViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/ic_tab_header_background"android:fillViewport="true"android:scrollbars="none" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="wrap_content"android:layout_height="wrap_content" ></TabWidget></HorizontalScrollView><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/ic_tab_body_background" ></FrameLayout>然后在drawable文件夾中添加2個selector,一個是用來設(shè)置選中的標簽的背景的,一個是用來設(shè)置選中的字體顏色變化的,代碼如下:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/tabbar_bg_selected" android:state_selected="true"></item><item android:drawable="@android:color/transparent" android:state_selected="false"></item> </selector> <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="#9d9d9d" android:state_selected="false"></item><item android:color="@android:color/black" android:state_selected="true"></item></selector>最后修改tabwighet.xml將樣式添加到標簽上:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:background="@drawable/selector_tab_header_item_background"android:gravity="center_horizontal"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_title"android:layout_width="200dp"android:layout_height="50dp"android:layout_gravity="center"android:gravity="center"android:singleLine="true"android:textColor="@drawable/selector_tab_header_item_txt_color" /> </LinearLayout>運行效果如下:
第五步,添加tabhost中標簽間的分割線
修改tabhost.xml的代碼為:
<TabWidgetandroid:id="@android:id/tabs"android:layout_width="wrap_content"android:layout_height="wrap_content"android:divider="@drawable/ic_tabbar_divider" ></TabWidget>效果圖如下:
以上幾步就完成了我們預(yù)定的3個功能。
下面是我在開發(fā)中遇到的幾個特使的問題,總結(jié)一下并列出我的解決方法:
1.標簽的寬度總是設(shè)置不成功——請參照第三步操作,這里注意設(shè)置最外邊的view的寬度是不能控制標簽的寬度的。
2.標簽間設(shè)置的自定義分割圖片總是不顯示——檢查AndroidManifest.xml中是不是設(shè)置了application的 android:theme屬性,如果設(shè)置成了@android:style/Theme.NoTitleBar,那么設(shè)置的分割是不能正常顯示的(初步 測試設(shè)置了與NoTitleBar有關(guān)的樣式的屬性都不能顯示),如果設(shè)置這個屬性是為了隱藏actionbar,建議修改成 @android:style/Theme.DeviceDefault.NoActionBar。
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhujiabin/p/4276492.html
總結(jié)
以上是生活随笔為你收集整理的Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二次周总结
- 下一篇: 同时启动多个Tomcat 和 Linux