Android碎片化Fragment实例一

一:内容概览
我们再android中所说的碎片化也就是针对不同设备的屏幕大小设计不同的适配方案所说的词语。一般而言我们就是在开发时针对不同屏幕分辨率的设备适配UI,比如手机和平板。在本小节,我们主要是介绍Fragment的最简单入门使用方法。
二:主要步骤:
2.1我们首先看一下我们的实例效果图:

这里写图片描述

我们可以看到在10英寸的屏幕平板中,将当前的Activity(也就是一个活动界面)嵌套两个Fragment;分为左边部分和右边部分。
2.2主要实现逻辑。
在MainActivity中我们嵌套两个fragment。那么下面我们就准备好 两个碎片fragment:left_fragment和right_fragment。并且新建left_fragment.java和right_fragment.java。简单的代码如下:
layout目录:
left_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@mipmap/stephenchou"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:text="这是左边"/>

</LinearLayout>

right_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
   android:background="@mipmap/car144205"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="200dp"
        android:text="这是右边"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="50sp"/>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
//把两个碎片加载到主页面
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.shanshui.ynu.myfragment.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <fragment
        android:id="@+id/right_fragment"
        android:name="com.shanshui.ynu.myfragment.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>/

</LinearLayout>

Java目录:
LeftFragment.java

package com.shanshui.ynu.myfragment;

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;

/**
 * Created by Administrator on 2017/12/26/026.
 */

public class LeftFragment extends Fragment {

   //唯一需要重写的onCreateView()方法
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
         //布局映射器映射到左边fragment
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}

RightFragment.java

package com.shanshui.ynu.myfragment;

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;

/**
 * Created by Administrator on 2017/12/26/026.
 */

public class RightFragment extends Fragment {
    //唯一需要重写的onCreateView()方法
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //布局映射器映射到右边fragment
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        //返回view值
        return view;
    }
}

MainActivity.java

    package com.shanshui.ynu.myfragment;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {

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

三:总结
在本实例中,我们的简单实例阐述了碎片化的使用流程。首先需要新建左右两边的xml文件。然后新建左右两个fragment的类文件,在此文件中我们只需要唯一的重写onCreateView方法。在这个方法中我们只需使用布局映射器映射到对应xml文件创建View对象。然后返回给当前重写函数。
我们这里实现的简单实例只是实现了fragment的创建过程,但是没有实现两个fragment的动态通信。下一期我们的内容是动态创建fragment.
欢迎继续关注,我的个人微信公众号:BlogShareCenter
这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_38321889/article/details/78907108