Android Tab Views - could not create tab content because could not find view with id

前端 未结 3 1102
粉色の甜心
粉色の甜心 2021-01-20 13:27

I am trying to create a tab view with will switch to a different content view by Id. Here is my tab activity:

public class TabViews extends TabA         


        
相关标签:
3条回答
  • 2021-01-20 14:06

    Your xml should have something like ,

      <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"
              >
    
        <FrameLayout  
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="test"/>   
    
        <TextView android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="test"/>
    
        <TextView android:id="@+id/view3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="test"/>
    
        </FrameLayout>
        </TabHost>
    

    examples: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/ http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android

    0 讨论(0)
  • 2021-01-20 14:20

    you should have TabHost and TabWidget in your xml layout, take a look at the Tab Layout example.

    0 讨论(0)
  • 2021-01-20 14:31

    you need add tabwidget in the xml.... Checkout, this is how to use tab widget :

    tabscroll.xml

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp">
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none">
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </HorizontalScrollView>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp" />
        </LinearLayout>
    </TabHost>
    
    
        public class Tabs5 extends TabActivity implements TabHost.TabContentFactory {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.tabs_scroll);
    
            final TabHost tabHost = getTabHost();
    
            for (int i=1; i <= 30; i++) {
                String name = "Tab " + i;
                tabHost.addTab(tabHost.newTabSpec(name)
                        .setIndicator(name)
                        .setContent(this));
            }
        }
    
        /** {@inheritDoc} */
        public View createTabContent(String tag) {
            final TextView tv = new TextView(this);
            tv.setText("Content for tab with tag " + tag);
            return tv;
        }
    }
    

    Courtesy : http://developer.android.com/resources/samples/ApiDemos/res/layout/tabs_scroll.html AND http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs5.html

    0 讨论(0)
提交回复
热议问题