fragment_text(Activity)

fragment_text(Activity)

package com.example.myapplication_one;

import android.annotation.SuppressLint;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class fragment_text extends AppCompatActivity implements View.OnClickListener {

    Button button1,button2,button3,button4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_text_layout);
        init();
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        fragment_text1 fragment_text1 = new fragment_text1();
        /*
           * 1、获取管理者
           * 2、开启事务
           * 3、实现功能:replace
           * 4、提交
         */
        FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_layout, fragment_text1);
        //fragmentTransaction.show();
        fragmentTransaction.commit();
    }

    private void init(){
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
    }

    @SuppressLint("ResourceType")
    public void onClick(View view){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (view.getId()){
            case R.id.button1://显示第一个Fragemnt
                fragmentTransaction.replace(R.id.fragment_layout,new fragment_text2());
                break;
            case R.id.button2://显示第2Fragemnt
                fragmentTransaction.replace(R.id.fragment_layout,new fragment_text3());
                break;
//            case R.id.button3://显示第3个Fragemnt
//                fragmentTransaction.replace(R.layout.fragment_text_layout4,new fragment_text2());
//                break;
//            case R.id.button4://显示第4个Fragemnt
//                fragmentTransaction.replace(R.layout.fragment_text_layout4,new fragment_text2());
//                break;
//            default:
//                break;
        }
        fragmentTransaction.addToBackStack(null);//返回栈
        fragmentTransaction.commit();
    }
}

fragment_text_layout.xml

fragment_text_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:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:background="#00ff00"
        android:text="主页面" />

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7"
        />



    <!--  include用于显示一个新的页面布局  -->
    <include
        android:id="@+id/include1"
        layout="@layout/fragment_text_layout2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:layout_gravity="bottom"
        />


</LinearLayout>

效果:

注意:include的引用,用于显示一个新的页面布局。

fragment_text_layout2.xml

fragment_text_layout2.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:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button4" />
</LinearLayout>

效果:

fragment_text1.java

fragment_text1.java

package com.example.myapplication_one;

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

public class fragment_text1  extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_layout3,container,false);
//        View view2 = inflater.inflate(R.layout.activity_list_item,null);

        return view;
    }
}

fragment_text_layout3.xml

fragment_text_layout3.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:orientation="vertical"
    android:background="#ffff00">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:text="=这是第一个Fragment" />
</LinearLayout>

效果:

fragment_text2.java

fragment_text2.java

package com.example.myapplication_one;

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

public class fragment_text2 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_layout4,container,false);
//        View view2 = inflater.inflate(R.layout.activity_list_item,null);

        return view;
    }
}

fragment_text_layout4.xml

fragment_text_layout4.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:orientation="vertical"
    android:background="#00ff00">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:text="=这是第2个Fragment" />
</LinearLayout>

效果:

fragment_text3.java

fragment_text3.java

package com.example.myapplication_one;

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

public class fragment_text3 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_layout5,container,false);
//        View view2 = inflater.inflate(R.layout.activity_list_item,null);

        return view;
    }
}

fragment_text_layout5.xml

fragment_text_layout5.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:orientation="vertical"
    android:background="#05CBE4">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:text="=这是第一个Fragment" />
</LinearLayout>

效果:

最终实现效果

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐