Android TabLayout with FrameLayout含代碼

Android TabLayout with FrameLayout

在之前的頁面中,我們使用TabLayout和ViewPager創(chuàng)建了一個滑動選項卡。愛掏網(wǎng) - it200.com在這里,我們將使用TabLayout和FrameLayout創(chuàng)建非滑動選項卡。愛掏網(wǎng) - it200.com

TabLayout的項目通過添加Android Support Design Widget的TabItem來實現(xiàn)。愛掏網(wǎng) - it200.com

TabLayout使用FrameLayout的示例

讓我們創(chuàng)建一個使用FrameLayout和Fragment的TabLayout示例。愛掏網(wǎng) - it200.com

文件:activity.xml

創(chuàng)建一個包含TabLayout和FrameLayout視圖組件的activity.xml文件。愛掏網(wǎng) - it200.com

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="tablayout.example.com.tablayoutwithframelayout.MainActivity">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#7367">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Home" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Php" />
    </android.support.design.widget.TabLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="455dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout">

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

文件:build.gradle

現(xiàn)在在build.gradle文件中添加了TabLayout的依賴庫。愛掏網(wǎng) - it200.com

implementation 'com.android.support:design:26.1.0'

文件:MainActivity.java

package tablayout.example.com.tablayoutwithframelayout;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    FrameLayout frameLayout;
    Fragment fragment = null;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout=(TabLayout)findViewById(R.id.tabLayout);
        frameLayout=(FrameLayout)findViewById(R.id.frameLayout);

        fragment = new HomeFragment();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.commit();

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
               // Fragment fragment = null;
                switch (tab.getPosition()) {
                    case 0:
                        fragment = new HomeFragment();
                        break;
                    case 1:
                        fragment = new JavaFragment();
                        break;
                    case 2:
                        fragment = new AndroidFragment();
                        break;
                    case 3:
                        fragment = new PhpFragment();
                        break;
                }
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.frameLayout, fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
}

現(xiàn)在為所有不同的選項卡創(chuàng)建不同的片段文件。愛掏網(wǎng) - it200.com

文件:HomeFragment.java

package tablayout.example.com.tablayoutwithframelayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

}

文件:fragment_home.xml

<FrameLayout 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"
    tools:context="tablayout.example.com.tablayoutwithframelayout.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/home_fragment" />

</FrameLayout>

文件: JavaFragment.java

package tablayout.example.com.tablayoutwithframelayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class JavaFragment extends Fragment {

    public JavaFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_java, container, false);
    }

}

文件: fragment_java.xml

<FrameLayout 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"
    tools:context="tablayout.example.com.tablayoutwithframelayout.JavaFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/java_fragment" />

</FrameLayout>

文件: AndroidFragment.java

package tablayout.example.com.tablayoutwithframelayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AndroidFragment extends Fragment {

    public AndroidFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_android, container, false);
    }

}

文件: fragment_android.xml

<FrameLayout 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"
    tools:context="tablayout.example.com.tablayoutwithframelayout.AndroidFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/android_fragment" />

</FrameLayout>

文件: PhpFragment.java

package tablayout.example.com.tablayoutwithframelayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PhpFragment extends Fragment {

    public PhpFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_php, container, false);
    }

}

文件:fragment_php.xml

<FrameLayout 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"
    tools:context="tablayout.example.com.tablayoutwithframelayout.PhpFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/php_fragment" />

</FrameLayout>

文件:strings.xml

<resources>
    <string name="app_name">TabLayout with FrameLayout</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="home_fragment">Home fragment</string>
    <string name="java_fragment">Java fragment</string>
    <string name="android_fragment">Android fragment</string>
    <string name="php_fragment">Php fragment</string>
</resources>

輸出

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 精品久久久久久中文字幕女 | 国产真实乱子伦视频播放| 健身私教干了好几次| 一本伊大人香蕉在线观看| 美女脱一净二净不带胸罩| 日本一区二区三区久久| 国产亚洲人成无码网在线观看| 久久精品国产一区二区电影| 黑人video| 日韩av无码成人精品国产| 国产国产人免费人成免费视频| 久久久国产精品| 舌头伸进去里面吃小豆豆| 成人国产激情福利久久精品| 午夜网站在线观看| 一区二区三区日韩精品| 精品xxxxxbbbb欧美中文| 女人18毛片a级毛片| 亚洲色偷偷综合亚洲av78| 97精品伊人久久久大香线蕉| 欧美高清色视频在线播放| 国产精品爆乳奶水无码视频| 亚洲aⅴ在线无码播放毛片一线天| 欧美日韩一区二区三区四区在线观看| 晚上看b站直播软件| 国产亚洲欧美在线播放网站| 中文在线最新版天堂| 男生和女生一起差差差很痛视频| 在线精品91青草国产在线观看| 亚洲日韩V无码中文字幕| 国内精品免费麻豆网站91麻豆| 日本成a人片在线观看网址| 可以直接看的毛片| a级日本高清免费看| 欧美国产日本高清不卡| 国产性猛交╳XXX乱大交| 中文字幕影片免费在线观看| 男女下面一进一出无遮挡se| 国产精品成人无码久久久久久| 久久综合九色综合网站| 色吊丝最新在线播放网站|