<TabHost
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="wrap_content"
android:id="@android:id/tabcontent"/>
</LinearLayout>
</TabHost>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/img_01"
android:id="@+id/img1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/img_02"
android:id="@+id/img2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/img_03"
android:id="@+id/img3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/img_04"
android:id="@+id/img4"/>
public class MainActivity extends AppCompatActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost=findViewById(android.R.id.tabhost);
tabHost.setup();//初始化控件
LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
//加载四个选项卡布局
inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
inflater.inflate(R.layout.tab3,tabHost.getTabContentView());
inflater.inflate(R.layout.tab4,tabHost.getTabContentView());
//为选项卡设置标题
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1").setContent(R.id.img1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2").setContent(R.id.img2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3").setContent(R.id.img3));
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("Tab4").setContent(R.id.img4));
}
}
效果图展示:
本文地址:https://blog.csdn.net/qq_33766045/article/details/107591399
如对本文有疑问, 点击进行留言回复!!
Execution failed for task ‘:app:checkDebugDuplicateClasses‘解决方案
Cleartext HTTP traffic to pic1.win4000.com not permitted不允许明文http访问解决
Android SDK 26以后ant编译错误解决办法(错误信息:taskdef A class needed by class com.android.ant.GetUiTargetTask)
android8.1 mtk camera hal各种操作流程
Android 软键盘在有scollview,纵向viewpager+recyclview实现列表,或者recyclview, 把布局顶上去的问题,保证背景不会发生变化
Android开发使用MateriDesign(一套全新的界面设计语言)
网友评论