FragmentTabHost + fragment 菜单栏(布局测试)

目录

activity_main.xml布局文件

MainActivity文件 

效果


activity_main.xml布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--经测试,若使用fragment_home1,菜单栏位置在底部。使用layout_weight="1"充满屏幕-->
    <FrameLayout
        android:id="@+id/fragment_home1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#3b3333">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="在FragmentTabHost上面的FrameLayout"/>
    </FrameLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/fragmenthost_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#77444444">

        <!--经测试,若使用fragment_home2,菜单栏覆盖在fragment上方(重合)-->
        <FrameLayout
            android:id="@+id/fragment_home2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#4c6d99">
            <!--未显示出来-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="在FragmentTabHost里面的FrameLayout"/>
        </FrameLayout>

    </android.support.v4.app.FragmentTabHost>

    <FrameLayout
        android:id="@+id/fragment_home"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#994c4c">
        <!--显示出来了,但使用FragmentTabHost动态加载的fragment并不和这里的TextView显示在同一个位置-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="在FragmentTabHost下面的FrameLayout"/>
    </FrameLayout>

</LinearLayout>

MainActivity文件 

package com.app.ch.fragmenttabhost;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.app.ch.fragmenttabhost.fragment.Fragment_display;
import com.app.ch.fragmenttabhost.fragment.Fragment_net;
import com.app.ch.fragmenttabhost.fragment.Fragment_sysinfo;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = LayoutInflater.from(this).inflate(R.layout.tabhost_systinfo, null);

        FragmentTabHost fragmentTabHost = findViewById(R.id.fragmenthost_home);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fragment_home);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("系统设置").setIndicator(view), Fragment_sysinfo.class, null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("网络设置").setIndicator("网络设置"), Fragment_net.class, null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("显示设置").setIndicator("显示设置"), Fragment_display.class, null);
        fragmentTabHost.setCurrentTab(2);
    }
}

效果

另外:

要修改菜单栏的高度,必须全都不使用默认的菜单样式。 使用自定义的view才能修改

猜你喜欢

转载自blog.csdn.net/liuyj_vv/article/details/88352027