我的Tab_Bar.class定义了定义选项卡如何打开子活动
对于Tab主机,我只使用一个Tab_bar.class
public class Tab_Bar extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
setTabs() ;
}
void setTabs()
{
addTab("My Profile", R.drawable.home_normal, MyProfile.class);
addTab("Search", R.drawable.search_normal, JobSearch.class);
addTab("Saved Jobs", R.drawable.starred, Saved_jobs.class);
addTab("Job Alert", R.drawable.job_match, JobAlert.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
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);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}}
我正在使用这些xml文件TabIndicator.xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/tab_indicator"
android:padding="5dp">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/icon"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
android:textSize="13sp"
/>
和Tab.xml
<?xml version="1.0" encoding="utf-8"?>
<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> 我正在为Tab主机使用Tab_bar.class和这些xml文件,但是我不知道打开选项卡主机子活动。我是android的新手。请帮助我,我如何才能开展儿童活动任何帮助都是非常感谢的。
对于我糟糕的英语我真的很抱歉
发布于 2014-01-08 18:42:39
这并不是你所需要的,但可能会有所帮助。我使用这个设置创建动态选项卡,然后对它们执行不同的操作。
protected void onCreate(Bundle savedInstanceState) {
...
final TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
int count;
for (count =0;count < 2;count++){
...
final int passedTabId = count;
NewTab.setContent(new TabHost.TabContentFactory()
{
public View createTabContent(String tag)
{
...
RelativeLayout layout = new RelativeLayout(getApplicationContext);
android.widget.RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
layout.setId(some ID);
layout.setBackgroundResource(R.drawable.room_background);
TextView dynText = new TextView(getApplicationContext);
...
layout.addView(dynText);
return layout;
// You can set onClickListeners, etc here and then assign them some functions you need
// You can also create different layouts for every tab according to the passedTabId
}
});
Tabs.addTab(NewTab);
}
}问题是,您不能简单地将另一个活动设置为在每个选项卡中运行。您需要为在该选项卡中创建的对象设置一些所需的函数,但是主活动保持不变。祝你好运:)
发布于 2014-01-08 07:38:33
首先,尝试使用片段来创建选项卡。
您正在使用的特性现在已不再推荐使用了。
我已经在Dynamically changing the fragments inside a fragment tab host?上发布了完整的帖子
请检查一下,如果您有任何问题,请告诉我。
简单地一个一个地休整每一个定义的步骤。
概念
我举一个简单的例子来说明这个片段到底是如何工作的。
以你床上的简单场景为例。你的床包括几个东西,如枕头,床单等。现在考虑一个案例,你的床单是脏的。那么你现在要做什么。你只需用一张新的纸来代替它,然后你就会睡得很紧:)
同样,当你需要改变你的UI时.然后这个片段被替换成一个新的片段。
就这样。现在你可以和我的职位一起去了。
谢谢!
https://stackoverflow.com/questions/20988505
复制相似问题