为安卓应用程序绘制自定义标签栏



我需要开发一个底部带有标签栏的应用程序,但我在Fragments方面有很多问题,Android文档展示了如何使用TabHost和Fragments。
因为没有显示任何关于如何正确处理每个选项卡堆栈的内容。

由于我可以转到选项卡 1,它是打开片段

A,然后我想深入并打开片段 B,并有可能将选项卡切换到选项卡 2,以及在选项卡 1 中切换回相同的片段 B。

有一些解决方案为每个选项卡创建一个片段活动,但对于该管理,您应该使用已弃用的 TabActivity。

所以也许我可以绘制我的标签栏并将其放在所有布局上,每次用户按下标签栏按钮时,我都会开始一个活动?

如果这是可能的,也许有些人已经实现了这个,或者可以展示一些教程如何绘制或使用这个?

谢谢。

在 XML down_tabs.xml中添加此代码

<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:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="0dip"
        android:layout_weight="1" />
    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent"        android:layout_height="wrap_content"
        android:layout_weight="0"  />
</LinearLayout>

并在活动类中添加选项卡规范为,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.down_tabs);
    initTabs();
}
private void initTabs() {
    // Set Calendar Tab
    // getTabWidget().setDividerDrawable(R.drawable.tab_divider);
    getTabWidget().setPadding(0, 0, 0, 0);
    addTab("", R.drawable.home_tab_drawable, CalendarUIActivity.class);
    addTab("", R.drawable.lucky_dates_drawable, LuckyDatesActivity.class);
    addTab("", R.drawable.life_stages_drawable, LifeStagesActivity.class);
    addTab("", R.drawable.find_items_drawable, FindItemsActivity.class);
    addTab("", R.drawable.more_tab_drawable, MoreActivity.class);
}
private void addTab(String labelId, int drawableId, Class<?> targetClass) {
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, targetClass);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
    View tabIndicator = LayoutInflater.from(this).inflate(
            R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);
    tabIndicator.setBackgroundResource(R.drawable.tab_backgroud);
    // //////////
    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);
}

我希望它能正常工作。

是的,很好的答案。碎片很容易工作。你知道的。。

<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:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent" android:layout_height="0dip"
                android:layout_weight="1" />
            <TabWidget android:id="@android:id/tabs"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="0" />
        </LinearLayout>
    </TabHost>

有关完整的教程,请访问

http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

最新更新