TabHost自定义标签页(二)

TabHostActivity.java
package com.example.a20200712;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.Text;

public class TabHostActivity extends AppCompatActivity {
    private TabHost tabHost = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_host_layout);

        tabHost = (TabHost) this.findViewById(R.id.tab_host_layout);
        tabHost.setup();

        TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("host_tab_layout_page1");
        //tabSpec1.setIndicator("第一页",getResources().getDrawable(R.drawable.spring1));
        tabSpec1.setIndicator(getPageView("第一页", R.drawable.spring1));
        tabSpec1.setContent(R.id.host_tab_layout_page1);
        tabHost.addTab(tabSpec1);

        TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("host_tab_layout_page2");
        // tabSpec2.setIndicator("第二页",getResources().getDrawable(R.drawable.spring1));
        tabSpec2.setIndicator(getPageView("第二页", R.drawable.spring1));
        tabSpec2.setContent(R.id.host_tab_layout_page2);
        tabHost.addTab(tabSpec2);

        TabHost.TabSpec tabSpec3 = tabHost.newTabSpec("host_tab_layout_page3");
        //tabSpec3.setIndicator("第三页",getResources().getDrawable(R.drawable.spring1));
        tabSpec3.setIndicator(getPageView("第三页", R.drawable.spring1));
        tabSpec3.setContent(R.id.host_tab_layout_page3);
        tabHost.addTab(tabSpec3);

        //默认打开哪个标签页
        tabHost.setCurrentTab(0);
    }

    private View getPageView(String subject, int resId) {
        View view = getLayoutInflater().inflate(R.layout.tab_host_head_page_layout, null);
        TextView textView = view.findViewById(R.id.tab_host_head_page_title);

        return view;
    }
}

主UI  

tab_host_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:theme="@android:style/Theme.Black"
    android:id="@+id/tab_host_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- 标签头 -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></TabWidget>

        <!-- 标签页 -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/host_tab_layout_page1"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="第一页"></TextView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/host_tab_layout_page2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="第二页"></TextView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/host_tab_layout_page3"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="第三页"></TextView>
            </LinearLayout>

        </FrameLayout>


    </LinearLayout>

</TabHost>

自定义头UI

tab_host_head_page_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tab_host_head_page_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/tab_host_selected"
        android:layout_gravity="center"
        android:layout_marginRight="1dp"
        android:textSize="20dp"></TextView>
</LinearLayout>

在drawable目录下建一个动态图

tab_host_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<item android:state_selected="true" android:drawable="@drawable/spring1"/>
<item android:state_selected="false"
    android:drawable="@drawable/java1" />
</selector>

猜你喜欢

转载自blog.csdn.net/m0_37622302/article/details/107641307