赞
踩
选项卡组件不能直接添加到xml中。
main.xml:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabhost" tools:context=".MainActivity"> <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"> </FrameLayout> </LinearLayout> </TabHost>
tab1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
android:src="@drawable/panda"/>
</LinearLayout>
tab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
android:src="@drawable/laugh"/>
</LinearLayout>
java:
package com.example.tabhost; import android.view.LayoutInflater; import android.widget.TabHost; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tabHost=findViewById(android.R.id.tabhost); //初始化 tabHost.setup(); //添加各个标签页,要添加标签页,需要: LayoutInflater inflater=LayoutInflater.from(this); inflater.inflate(R.layout.tab1,tabHost.getTabContentView()); inflater.inflate(R.layout.tab2,tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("精选表情").setContent(R.id.left)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("投稿表情").setContent(R.id.right)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。