为Android应用程序绘制自定义标签栏
问题描述:
我需要在底部开发一个带有标签栏的应用程序,但我有许多Fragments问题,其中Android文档显示如何使用TabHost和Fragments。
因为没有显示任何内容,所以关于如何正确使用每个标签堆栈。为Android应用程序绘制自定义标签栏
因为我可以去tab1它是开放的片段A,那么我想进入深层和开放的片段B,并有可能切换到tab2,以及切换回看到tab1中相同的片段B.
有一些解决方案为每个选项卡创建一个FragmentActivity,但是对于该管理您应该使用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);
}
我希望它会正常工作。
答
烨良好answer..fragments容易work..u知道..
<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/
我曾撰文指出TabActivity是不推荐使用,而应该使用Fragments。我现在采用这种方法,但它不适合我。但是,谢谢。 – Streetboy 2012-04-17 07:53:41
很好的答案谢谢! – 2012-04-23 05:11:07