本帖最后由 liu 于 2018-12-17 11:54 编辑
Fragment的用法
本篇说一下怎么将Fragment添加到Activity中,有两种方法:
先新建两个Fragment(LeftFragment,RightFragment):
LeftFragment
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LeftFragment extends Fragment {
public LeftFragment() {
// 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_left, container, false);
}
}
fragment_left.xml[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"
android:background="@color/colorAccent"
tools:context="com.company.helloworld.firstapplication.LeftFragment">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="左边Fragment"
android:textColor="@android:color/white" />
</FrameLayout>
RightFragment[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class RightFragment extends Fragment {
public RightFragment() {
// 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_right, container, false);
}
}
fragment_right.xml[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"
android:background="@color/colorPrimary"
tools:context="com.company.helloworld.firstapplication.RightFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="右边Fragment"
android:gravity="center"
android:textColor="@android:color/white" />
</FrameLayout>
第一种方法(在布局中静态加载):
主布局activity_main.xml中:
[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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--android:name指定加载的Fragment-->
<fragment
android:id="@+id/left_fragment"
android:name="com.company.helloworld.firstapplication.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_fragment"
android:name="com.company.helloworld.firstapplication.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
运行结果如下图:
第二种方法(动态加载):
主布局activity_main.xml:
[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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LeftFragment leftFragment = new LeftFragment();
FragmentManager supportFragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_content, leftFragment).commit();
}
}
运行结果:
因为android 3.0以下版本没有fragment的api,所以必须借助V4包里面的getSupportFragmentManager()方法来间接获取FragmentManager()对象,同时继承FragmentActivity
而android 3.0版本之后有了fragment的API,所以通过getFragmentManager()方法直接就可以获取到FragmentManager对象,并且继承自Activity就可以嵌入使用fragment
注意:每个Fragment和Activity导的包要保持一致,例如都是import android.app或者都是import android.support.v4.app,不然容易出错
|