我想知道以编程方式向标签主机添加标签的最简单方法(例如,通过按下按钮)。换句话说,没有在XML文件中对其结构进行硬编码。
答案 0 :(得分:1)
以编程方式创建选项卡并使用内容填充选项卡的一种方法是:
TabSpec.setContent
方法单独Layout.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Kablam"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/kablamTextView" />
</LinearLayout>
TabContent Factory类(定义为Activity
=&gt; this
中的内部类是活动实例)
private class MyTabContentFactory implements TabContentFactory {
public View CreateTabContent(string tag) {
View view = this.getLayoutInflater()
.inflate(R.layout.Layout, (ViewGroup)this.FindViewById(R.id.tabHost1), false);
((TextView)view.findViewById(R.id.kablamTextView))
.setText("Some sentence which can be generated dynamically");
return view;
}
}
最后以Hemendra Sharma的答案为基础,使用标签工厂定义内容
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec tab1 = tabHost.newTabSpec("Tab_Name");
tab1.setIndicator("Tab 1");
tab1.setContent(new MyTabContentFactory());
tabHost.addTab(tab1);
答案 1 :(得分:0)
这是一种以编程方式在TabHost上添加标签的简单方法。
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec tab1 = tabHost.newTabSpec("Tab_Name");
View view = getLayoutInflater().inflate(R.layout.tab_indicator,
myLayout, false);
tab1.setIndicator(view);
Intent i = new Intent(getApplicationContext(), MyActivity.class);
tab1.setContent(i);
tabHost.addTab(tab1);
祝你好运。 :)
答案 2 :(得分:0)
按钮点击事件,试试这个
myTabHost =(TabHost) findViewById(R.id.tabhostId);
mytabhost.setup();
TabSpec spec = mytabhost.newTabSpec("tab_creation");
spec.setIndicator("TAB_NAME",getResources().getDrawable(android.R.drawable.ic_menu_add));// text and image of tab
spec.setContent(R.id.layout_of_tab); // layout of tab
mytabhost.addTab(spec);