Tab simple switching TabHost

Inherited TabActivity
obtained from TabActivity by getTabHost () method TabHost, then labels the content layout:
. 1, must be set TabHost android: id is @android: ID / TabHost
2, must be set TabWidget android: id is @android: ID / tabs
. 3 , FrameLayout must set android: id is @android: id / tabcontent
otherwise similar error will occur:


//layout

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabhost" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs" ></TabWidget> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@android:id/tabcontent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/widget_layout_red" android:background="#ff0000" android:orientation="vertical" ></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/widget_layout_yellow" android:background="#FCD209" android:orientation="vertical" ></LinearLayout> </FrameLayout> </LinearLayout> </TabHost>Inheritance TabActivity
public class MainActivity extends TabActivity { private TabHost tabhost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo); //从TabActivity上面获取放置Tab的TabHost tabhost = getTabHost(); //两种添加方式 tabhost.addTab(tabhost //创建新标签one .newTabSpec("one") //设置标签标题 .setIndicator("红色") //设置该标签的布局内容 .setContent(R.id.widget_layout_red));
tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.widget_layout_yellow)); } }

 

Guess you like

Origin www.cnblogs.com/sunjian43792901/p/11130595.html