为什么这个 TextView 出现在我的两个 TabPage 上?应该只在第一个
我有一个 TextView
,它只应该显示在第一个 TabPage
(其中包含 ScrollView
)上,但由于某种原因它显示了在我的两个 TabPages 上,而 TableLayout 仅显示在第一个 TabPage 中,就像它应该的那样。我不知道为什么......这是我的 XML 文件:有问题的 TextView
是 ScrollView 中的“primaryInfo”。
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Primary Info"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:id="@+id/piTextView"
android:background="#595959"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/specsTab"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/photosTab">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/thumbnailGallery"
android:id="@+id/selectedPhoto"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageProgressLayout"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"
android:indeterminate="true"
android:layout_marginTop="5dp"
android_layout_marginLeft="20dp"
android_layout_marginBottom="20dp"
android:id="@+id/galleryProgress"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/loadingTextView"
android:text="Loading images..."
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:textSize="13sp"/>
</LinearLayout>
<Gallery
android:id="@+id/thumbnailGallery"
android:layout_above="@+id/galleryProgress"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
编辑
选项卡设置代码:
public void tabSetup()
{
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TextView tv1 = new TextView(this);
tv1.setText("Specs");
tv1.setHeight(50);
TextView tv2 = new TextView(this);
tv2.setText("Photos");
tv2.setHeight(50);
TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.specsTab);
spec1.setIndicator("Specs");
TabSpec spec2=tabHost.newTabSpec("Photos");
spec2.setIndicator("Photos");
spec2.setContent(R.id.photosTab);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
}
I have a TextView
that is only supposed to show up on the first TabPage
(which contains the ScrollView
), but for some reason it shows up on both of my TabPages
, whereas the TableLayout only shows up in the first TabPage like it should. I have no idea why...here is my XML file: The TextView
in question is the 'primaryInfo' in the ScrollView.
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Primary Info"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:id="@+id/piTextView"
android:background="#595959"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/specsTab"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/photosTab">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/thumbnailGallery"
android:id="@+id/selectedPhoto"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageProgressLayout"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"
android:indeterminate="true"
android:layout_marginTop="5dp"
android_layout_marginLeft="20dp"
android_layout_marginBottom="20dp"
android:id="@+id/galleryProgress"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/loadingTextView"
android:text="Loading images..."
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:textSize="13sp"/>
</LinearLayout>
<Gallery
android:id="@+id/thumbnailGallery"
android:layout_above="@+id/galleryProgress"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
edit
Tab setup code:
public void tabSetup()
{
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TextView tv1 = new TextView(this);
tv1.setText("Specs");
tv1.setHeight(50);
TextView tv2 = new TextView(this);
tv2.setText("Photos");
tv2.setHeight(50);
TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.specsTab);
spec1.setIndicator("Specs");
TabSpec spec2=tabHost.newTabSpec("Photos");
spec2.setIndicator("Photos");
spec2.setContent(R.id.photosTab);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,当我更改布局时,我忘记更改传递给 setContent 方法的视图,因为该选项卡页的顶级视图发生了变化。这是我更改布局之前的情况(它最初是有效的):
这就是我上面发布的当前布局所需要的:
问题已解决。感谢杰克让我发布代码,因为那是我终于注意到它的时候;)
Well, the problem was that when I changed the layout, I forgot to change the View that I passed into the
setContent
method since the top level view for that tab page changed. Here is what it was before I changed the layout (and it worked originally):and this is what it needed to be with the current layout that I posted above:
Problem solved. Credit to Jack for getting me to post the code since that is when I finally noticed it ;)