Android的Frangment的第一种声明方式

Android的Frangment的第一种声明方式

实际效果图如下:

image.png

项目结构图如下:

image.png

fragment1:
package com.demo.fragementfirst;

import android.content.Context;
import android.net.Uri;
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 fragment1 extends Fragment {

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


//        通过打气筒把一个布局转换成一个view对象
        View view = inflater.inflate(
                R.layout.fragment_fragment1, null
        );


        return view;


    }
}
MainActivity:
package com.demo.fragementfirst;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

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

    }
}
MyFragment:
package com.demo.fragementfirst;


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


//定义fragment
public class MyFragment extends Fragment {


    //    当第一次画ui的时候调用,通过这个方法可以让fragment显示自己的布局内容
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        通过打气筒把一个布局转换成一个view对象
        View view = inflater.inflate(
                R.layout.myfragment, null
        );


        return view;

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


    <!--viewgroup可以有自己的孩子
    通过 oncreateview 这个方法 fragment可以加载自己的布局


    -->
    <fragment
        android:name="com.demo.fragementfirst.MyFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        >
    </fragment>

    <fragment
        android:name="com.demo.fragementfirst.fragment1"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        ></fragment>



</LinearLayout>
fragment_fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<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=".fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是第二个fragemnt" />

</FrameLayout>
myfragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<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=".fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D64B28"
        android:textColor="#FFFFFF"
        android:text="这个是第一个fragment" />

</FrameLayout>

猜你喜欢

转载自www.cnblogs.com/charlypage/p/10351921.html