Почему это текстовое представление отображается на обеих моих страницах с вкладками? Оно должно быть только на первом

#android #xml #layout #tabs #android-tabhost

#Android #xml #макет #вкладки #android-tabhost

Вопрос:

У меня есть a TextView , который должен отображаться только на первой TabPage (которая содержит ScrollView ), но по какой-то причине он отображается на обеих моих TabPages страницах, тогда как TableLayout отображается только на первой странице с вкладками, как и должно быть. Я понятия не имею, почему…вот мой XML-файл: TextView речь идет о «primaryInfo» в 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>
  

Редактировать

Код настройки вкладки:

     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);
}
  

Комментарии:

1. Вы знаете, я думал об этом больше, я не совсем понимаю ваш макет, если вы не запускаете новые действия для каждой вкладки, как тогда вы управляете «вкладками»? Как вкладки добавляются на ваш хост вкладок? Требуется больше кода, чтобы понять, как вы управляете вкладками, если вы не используете встроенную функциональность.

2. Я добавил код, используемый для настройки вкладок

3. О черт, я думаю, я понял это … одну секунду — я изменил макет с тех пор, как я изначально написал этот код. Я совершенно забыл о setContent()

Ответ №1:

Ну, проблема заключалась в том, что когда я изменил макет, я забыл изменить представление, которое я передал в setContent метод, поскольку изменился вид верхнего уровня для этой страницы вкладок. Вот что было до того, как я изменил макет (и он работал изначально):

 TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.specsTab);
spec1.setIndicator("Specs");
  

и это то, что нужно было для текущего макета, который я опубликовал выше:

 TabSpec spec1=tabHost.newTabSpec("Specs");
spec1.setContent(R.id.scrollView);
spec1.setIndicator("Specs");
  

Проблема решена. Спасибо Джеку за то, что он заставил меня опубликовать код, так как именно тогда я, наконец, заметил это 😉