今天学习了选项卡,为记账本的分类做了准备。主登录界面进行了优化,但仍未实现各个组件之间的跳转。
选项卡
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 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_height="wrap_content" android:layout_width="match_parent" android:id="@android:id/tabs" ></TabWidget> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" 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:orientation="vertical" android:id="@+id/left" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bg"/> </LinearLayout>
tab2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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/img"/> </LinearLayout>
MainActivity.java
package com.example.xuanxiang; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost 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)); } }
优化美化后的主界面..........