参与新波士顿教程(如果你知道有人试图学习android,我强烈推荐它们)。我正在制作标签。我有一些设置,但您可以看到并与“顶部”后面的所有选项卡内容进行交互。我怎么阻止它?我尝试在构成每个标签的每个线性布局中添加背景,但所做的只是让他们看到不同颜色的背景。底部的图像显示顶部的左侧选项卡,但显示第二个和第三个选项卡的按钮/文本视图。我需要每个标签只显示该标签中的内容,而不是顶部标签后面的标签。
爪哇:
package com.thenewboston.psest328;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class Tabs extends Activity implements OnClickListener {
TabHost th;
Button newTab, bStart, bStop;
TextView showResults;
long start;
long stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
th = (TabHost)findViewById(R.id.tabhost);
newTab = (Button)findViewById(R.id.bAddTab);
bStart = (Button)findViewById(R.id.bStartWatch);
bStop = (Button)findViewById(R.id.bStopWatch);
showResults = (TextView)findViewById(R.id.tvShowResults);
th.setup();
newTab.setOnClickListener(this);
bStart.setOnClickListener(this);
bStop.setOnClickListener(this);
TabSpec spec = th.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("StopWatch");
th.addTab(spec);
spec = th.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Tab 2");
th.addTab(spec);
spec = th.newTabSpec("tag3");
spec.setContent(R.id.tab2);
spec.setIndicator("Add Tab");
th.addTab(spec);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()) {
case R.id.bAddTab:
TabSpec ourSpec = th.newTabSpec("tag1");
ourSpec.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
TextView text = new TextView(Tabs.this);
text.setText("You've created a new tab");
return text;
}
});
ourSpec.setIndicator("New");
th.addTab(ourSpec);
break;
case R.id.bStartWatch:
start = 0;
start = System.currentTimeMillis();
break;
case R.id.bStopWatch:
stop = System.currentTimeMillis();
if(start != 0) {
long result = stop - start;
showResults.setText(Long.toString(result));
}
break;
}
}
}
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" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bStartWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/bStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
<TextView
android:id="@+id/tvShowResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 3" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bAddTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Tab" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
它看起来像什么: