Android Fragment碎片

首先给它一个主步局,就按照微信那种是形式做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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=".MainActivity">

    <LinearLayout
        android:id="@+id/layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="650dp">
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioGroup
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RadioButton
                android:layout_weight="1"
                android:id="@+id/r1"
                android:button="@null"
                android:text="信息"
                android:textColor="@color/colorAccent"
                android:checked="true"
                android:textSize="24dp"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
            </RadioButton>
            <RadioButton
                android:layout_weight="1"
                android:id="@+id/r2"
                android:text="联系人"
                android:button="@null"
                android:textSize="24dp"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
            </RadioButton>
            <RadioButton
                android:layout_weight="1"
                android:id="@+id/r3"
                android:text="我"
                android:button="@null"
                android:textSize="24dp"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
            </RadioButton>
        </RadioGroup>
    </LinearLayout>
</LinearLayout>

其次就是你要的界面,就自定义几个布局

然后自己创建相对应的几个类:

继承Fragment,重写onCreateView决定Fragment的布局

package com.example.day13;

import android.os.Bundle;
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 MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return View.inflate(getActivity(), R.layout.f3,null);
    }
}

返回值的第一个参数:自适应的主类,第二个参数:相对应的布局文件,第三个参数:null

最后就是主类:

package com.example.day13;

import android.graphics.Color;
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;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {

    private RadioButton message;
    private RadioButton couat;
    private RadioButton me;

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

        message = findViewById(R.id.r1);
        couat = findViewById(R.id.r2);
        me = findViewById(R.id.r3);

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.layout, new Message());//replace这个方法表示删除其他的,添加当前的
        transaction.commit();//***********

        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout, new Message());//replace这个方法表示删除其他的,添加当前的
                transaction.commit();//***********
                message.setTextColor(Color.RED);
                couat.setTextColor(Color.BLACK);
                me.setTextColor(Color.BLACK);
            }
        });

        couat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout, new couatFragment());//replace这个方法表示删除其他的,添加当前的
                transaction.commit();//***********
                message.setTextColor(Color.BLACK);
                couat.setTextColor(Color.RED);
                me.setTextColor(Color.BLACK);
            }
        });

        me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout, new MyFragment());//replace这个方法表示删除其他的,添加当前的
                transaction.commit();//***********
                message.setTextColor(Color.BLACK);
                couat.setTextColor(Color.BLACK);
                me.setTextColor(Color.RED);
            }
        });
    }
}

其实就四行代码:

FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.layout, new Message());//replace这个方法表示删除其他的,添加当前的
                transaction.commit();//***********

其他的就是一些点击事件,点击那个就跳转到那个界面(可以这样理解)

                                                                                                                                 好想知道这个博客是什么时候更新的...

猜你喜欢

转载自blog.csdn.net/LIXIAONA_1101/article/details/81101058