viewpager懶載入
阿新 • • 發佈:2018-11-14
viewpager懶載入
什麼是懶載入:viewpager會預設預載入當前介面左右的fragment,為了防止其他需要預載入並且不可見的fragment與當前可見的fragment在載入資料時搶佔cpu和記憶體資源,預載入的fragment只有當其可見時才去載入資料,這樣有利於提高使用者體驗
效果圖如下:每個介面只有在可見時才會隨機一個整數且只隨機一次
涉及到以下幾點知識點:
- 通過setOffscreenPageLimit方法設定offscreenPageLimit個數來控制快取可見介面左右兩邊各快取的page數目 ,總數目為n*2+1;當載入的page超過這個數目時會用FragmentManager來釋放fragment;offscreenPageLimit預設值為1
- FragmentPagerAdapter當快取的介面超過n*2+1時候,會對介面進行回收,只會回收view,不回收Fragment,見圖 執行了Fragment的onDestoryView方法,並沒有執行onDestory方法,下次使用時候會重新初始化view;適合fragment數目較少的情況,因為其fragment並沒有被回收,一直佔用著記憶體
- setUserVisibleHint(boolean isVisibleToUser)的引數isVisibleToUser為true時候表示當前fragment對使用者可見,為false時表示對使用者不可見
- FragmentStatePagerAdapter當快取的介面超過n*2+1時候,會對介面進行回收,會把整個Fragment進行回收,見圖 執行了Fragment的onDestory方法,下次使用時會重新建立一個fragment;適合fragment數目比較多
Adapter為FragmentPagerAdapter時,日誌如下:
FragmentPagerAdapter --------------------------------------app啟動開啟第0個fragment------------------------------------------------- 09-20 22:06:00.468 5366-5366/com.hj.test D/生命週期_Fragment0: setUserVisibleHintfalse 09-20 22:06:00.468 5366-5366/com.hj.test D/生命週期_Fragment1: setUserVisibleHintfalse 09-20 22:06:00.468 5366-5366/com.hj.test D/生命週期_Fragment0: setUserVisibleHinttrue 09-20 22:06:00.469 5366-5366/com.hj.test D/生命週期_Fragment0: onAttach 09-20 22:06:00.469 5366-5366/com.hj.test D/生命週期_Fragment1: onAttach 09-20 22:06:00.469 5366-5366/com.hj.test D/生命週期_Fragment0: onCreateView 09-20 22:06:00.472 5366-5366/com.hj.test D/生命週期_Fragment0: lazyLoad第0個介面準備載入資料 09-20 22:06:00.472 5366-5366/com.hj.test D/生命週期_Fragment0: onActivityCreated 09-20 22:06:00.472 5366-5366/com.hj.test D/生命週期_Fragment0: onViewStateRestored 09-20 22:06:00.472 5366-5366/com.hj.test D/生命週期_Fragment0: onStart 09-20 22:06:00.472 5366-5366/com.hj.test D/生命週期_Fragment0: onResume 09-20 22:06:00.473 5366-5366/com.hj.test D/生命週期_Fragment1: onCreateView 09-20 22:06:00.475 5366-5366/com.hj.test D/生命週期_Fragment1: onActivityCreated 09-20 22:06:00.475 5366-5366/com.hj.test D/生命週期_Fragment1: onViewStateRestored 09-20 22:06:00.475 5366-5366/com.hj.test D/生命週期_Fragment1: onStart 09-20 22:06:00.475 5366-5366/com.hj.test D/生命週期_Fragment1: onResume --------------------------------------由第0個fragment滑動到第1個fragment--------------------------------------------------- 09-20 22:06:07.455 5366-5366/com.hj.test D/生命週期_Fragment2: setUserVisibleHintfalse 09-20 22:06:07.455 5366-5366/com.hj.test D/生命週期_Fragment0: setUserVisibleHintfalse 09-20 22:06:07.455 5366-5366/com.hj.test D/生命週期_Fragment1: lazyLoad第1個介面準備載入資料 09-20 22:06:07.458 5366-5366/com.hj.test D/生命週期_Fragment1: setUserVisibleHinttrue 09-20 22:06:07.459 5366-5366/com.hj.test D/生命週期_Fragment2: onAttach 09-20 22:06:07.460 5366-5366/com.hj.test D/生命週期_Fragment2: onCreateView 09-20 22:06:07.469 5366-5366/com.hj.test D/生命週期_Fragment2: onActivityCreated 09-20 22:06:07.469 5366-5366/com.hj.test D/生命週期_Fragment2: onViewStateRestored 09-20 22:06:07.469 5366-5366/com.hj.test D/生命週期_Fragment2: onStart 09-20 22:06:07.470 5366-5366/com.hj.test D/生命週期_Fragment2: onResume --------------------------------------由第1個fragment滑動到第2個fragment--------------------------------------------------- 09-20 22:06:21.633 5366-5366/com.hj.test D/生命週期_Fragment3: setUserVisibleHintfalse 09-20 22:06:21.634 5366-5366/com.hj.test D/生命週期_Fragment1: setUserVisibleHintfalse 09-20 22:06:21.634 5366-5366/com.hj.test D/生命週期_Fragment2: lazyLoad第2個介面準備載入資料 09-20 22:06:21.637 5366-5366/com.hj.test D/生命週期_Fragment2: setUserVisibleHinttrue 09-20 22:06:21.638 5366-5366/com.hj.test D/生命週期_Fragment3: onAttach 09-20 22:06:21.639 5366-5366/com.hj.test D/生命週期_Fragment0: onPause 09-20 22:06:21.639 5366-5366/com.hj.test D/生命週期_Fragment0: onStop 09-20 22:06:21.640 5366-5366/com.hj.test D/生命週期_Fragment0: onDestroyView 09-20 22:06:21.642 5366-5366/com.hj.test D/生命週期_Fragment3: onCreateView 09-20 22:06:21.653 5366-5366/com.hj.test D/生命週期_Fragment3: onActivityCreated 09-20 22:06:21.654 5366-5366/com.hj.test D/生命週期_Fragment3: onViewStateRestored 09-20 22:06:21.654 5366-5366/com.hj.test D/生命週期_Fragment3: onStart 09-20 22:06:21.654 5366-5366/com.hj.test D/生命週期_Fragment3: onResume --------------------------------------由第2個fragment滑動到第1個fragment--------------------------------------------------- 09-20 22:06:29.427 5366-5366/com.hj.test D/生命週期_Fragment0: setUserVisibleHintfalse 09-20 22:06:29.428 5366-5366/com.hj.test D/生命週期_Fragment2: setUserVisibleHintfalse 09-20 22:06:29.428 5366-5366/com.hj.test D/生命週期_Fragment1: setUserVisibleHinttrue 09-20 22:06:29.430 5366-5366/com.hj.test D/生命週期_Fragment0: onCreateView 09-20 22:06:29.440 5366-5366/com.hj.test D/生命週期_Fragment0: lazyLoad第0個介面準備載入資料 09-20 22:06:29.440 5366-5366/com.hj.test D/生命週期_Fragment0: onActivityCreated 09-20 22:06:29.441 5366-5366/com.hj.test D/生命週期_Fragment0: onViewStateRestored 09-20 22:06:29.442 5366-5366/com.hj.test D/生命週期_Fragment3: onPause 09-20 22:06:29.442 5366-5366/com.hj.test D/生命週期_Fragment3: onStop 09-20 22:06:29.442 5366-5366/com.hj.test D/生命週期_Fragment3: onDestroyView 09-20 22:06:29.443 5366-5366/com.hj.test D/生命週期_Fragment0: onStart 09-20 22:06:29.443 5366-5366/com.hj.test D/生命週期_Fragment0: onResume --------------------------------------由第1個fragment滑動到第0個fragment--------------------------------------------------- 09-20 22:06:37.288 5366-5366/com.hj.test D/生命週期_Fragment1: setUserVisibleHintfalse 09-20 22:06:37.288 5366-5366/com.hj.test D/生命週期_Fragment0: setUserVisibleHinttrue 09-20 22:06:37.291 5366-5366/com.hj.test D/生命週期_Fragment2: onPause 09-20 22:06:37.291 5366-5366/com.hj.test D/生命週期_Fragment2: onStop 09-20 22:06:37.291 5366-5366/com.hj.test D/生命週期_Fragment2: onDestroyView
Adapter為FragmentStatePagerAdapter時,日誌如下:
FragmentStatePagerAdapter
--------------------------------------app啟動開啟第0個fragment-------------------------------------------------
09-20 21:44:18.281 8635-8635/com.hj.test D/生命週期_Fragment0: setUserVisibleHintfalse
09-20 21:44:18.281 8635-8635/com.hj.test D/生命週期_Fragment1: setUserVisibleHintfalse
09-20 21:44:18.281 8635-8635/com.hj.test D/生命週期_Fragment0: setUserVisibleHinttrue
09-20 21:44:18.281 8635-8635/com.hj.test D/生命週期_Fragment0: onAttach
09-20 21:44:18.281 8635-8635/com.hj.test D/生命週期_Fragment1: onAttach
09-20 21:44:18.282 8635-8635/com.hj.test D/生命週期_Fragment0: onCreateView
09-20 21:44:18.285 8635-8635/com.hj.test D/生命週期_Fragment0: lazyLoad第0個介面準備載入資料
09-20 21:44:18.286 8635-8635/com.hj.test D/生命週期_Fragment0: onActivityCreated
09-20 21:44:18.286 8635-8635/com.hj.test D/生命週期_Fragment0: onViewStateRestored
09-20 21:44:18.286 8635-8635/com.hj.test D/生命週期_Fragment0: onStart
09-20 21:44:18.286 8635-8635/com.hj.test D/生命週期_Fragment0: onResume
09-20 21:44:18.286 8635-8635/com.hj.test D/生命週期_Fragment1: onCreateView
09-20 21:44:18.289 8635-8635/com.hj.test D/生命週期_Fragment1: onActivityCreated
09-20 21:44:18.289 8635-8635/com.hj.test D/生命週期_Fragment1: onViewStateRestored
09-20 21:44:18.289 8635-8635/com.hj.test D/生命週期_Fragment1: onStart
09-20 21:44:18.289 8635-8635/com.hj.test D/生命週期_Fragment1: onResume
--------------------------------------由第0個fragment滑動到第1個fragment---------------------------------------------------
09-20 21:45:11.405 8635-8635/com.hj.test D/生命週期_Fragment2: setUserVisibleHintfalse
09-20 21:45:11.406 8635-8635/com.hj.test D/生命週期_Fragment0: setUserVisibleHintfalse
09-20 21:45:11.406 8635-8635/com.hj.test D/生命週期_Fragment1: lazyLoad第1個介面準備載入資料
09-20 21:45:11.409 8635-8635/com.hj.test D/生命週期_Fragment1: setUserVisibleHinttrue
09-20 21:45:11.411 8635-8635/com.hj.test D/生命週期_Fragment2: onAttach
09-20 21:45:11.413 8635-8635/com.hj.test D/生命週期_Fragment2: onCreateView
09-20 21:45:11.427 8635-8635/com.hj.test D/生命週期_Fragment2: onActivityCreated
09-20 21:45:11.428 8635-8635/com.hj.test D/生命週期_Fragment2: onViewStateRestored
09-20 21:45:11.428 8635-8635/com.hj.test D/生命週期_Fragment2: onStart
09-20 21:45:11.428 8635-8635/com.hj.test D/生命週期_Fragment2: onResume
--------------------------------------由第1個fragment滑動到第2個fragment---------------------------------------------------
09-20 21:45:50.422 8635-8635/com.hj.test D/生命週期_Fragment0: onSaveInstanceState
09-20 21:45:50.426 8635-8635/com.hj.test D/生命週期_Fragment3: setUserVisibleHintfalse
09-20 21:45:50.427 8635-8635/com.hj.test D/生命週期_Fragment1: setUserVisibleHintfalse
09-20 21:45:50.427 8635-8635/com.hj.test D/生命週期_Fragment2: lazyLoad第2個介面準備載入資料
09-20 21:45:50.431 8635-8635/com.hj.test D/生命週期_Fragment2: setUserVisibleHinttrue
09-20 21:45:50.432 8635-8635/com.hj.test D/生命週期_Fragment3: onAttach
09-20 21:45:50.433 8635-8635/com.hj.test D/生命週期_Fragment0: onPause
09-20 21:45:50.434 8635-8635/com.hj.test D/生命週期_Fragment0: onStop
09-20 21:45:50.434 8635-8635/com.hj.test D/生命週期_Fragment0: onDestroyView
09-20 21:45:50.436 8635-8635/com.hj.test D/生命週期_Fragment0: onDestroy
09-20 21:45:50.436 8635-8635/com.hj.test D/生命週期_Fragment0: onDetach
09-20 21:45:50.438 8635-8635/com.hj.test D/生命週期_Fragment3: onCreateView
09-20 21:45:50.450 8635-8635/com.hj.test D/生命週期_Fragment3: onActivityCreated
09-20 21:45:50.451 8635-8635/com.hj.test D/生命週期_Fragment3: onViewStateRestored
09-20 21:45:50.451 8635-8635/com.hj.test D/生命週期_Fragment3: onStart
09-20 21:45:50.451 8635-8635/com.hj.test D/生命週期_Fragment3: onResume
--------------------------------------由第2個fragment滑動到第1個fragment---------------------------------------------------
09-20 21:59:12.714 27188-27188/com.hj.test D/生命週期_Fragment0: setUserVisibleHintfalse
09-20 21:59:12.715 27188-27188/com.hj.test D/生命週期_Fragment3: onSaveInstanceState
09-20 21:59:12.717 27188-27188/com.hj.test D/生命週期_Fragment2: setUserVisibleHintfalse
09-20 21:59:12.718 27188-27188/com.hj.test D/生命週期_Fragment1: setUserVisibleHinttrue
09-20 21:59:12.719 27188-27188/com.hj.test D/生命週期_Fragment0: onAttach
09-20 21:59:12.720 27188-27188/com.hj.test D/生命週期_Fragment3: onPause
09-20 21:59:12.720 27188-27188/com.hj.test D/生命週期_Fragment3: onStop
09-20 21:59:12.720 27188-27188/com.hj.test D/生命週期_Fragment3: onDestroyView
09-20 21:59:12.722 27188-27188/com.hj.test D/生命週期_Fragment3: onDestroy
09-20 21:59:12.723 27188-27188/com.hj.test D/生命週期_Fragment3: onDetach
09-20 21:59:12.723 27188-27188/com.hj.test D/生命週期_Fragment0: onCreateView
09-20 21:59:12.737 27188-27188/com.hj.test D/生命週期_Fragment0: lazyLoad第0個介面準備載入資料
09-20 21:59:12.737 27188-27188/com.hj.test D/生命週期_Fragment0: onActivityCreated
09-20 21:59:12.739 27188-27188/com.hj.test D/生命週期_Fragment0: onViewStateRestored
09-20 21:59:12.739 27188-27188/com.hj.test D/生命週期_Fragment0: onStart
09-20 21:59:12.739 27188-27188/com.hj.test D/生命週期_Fragment0: onResume
--------------------------------------由第1個fragment滑動到第0個fragment---------------------------------------------------
09-20 22:00:30.527 27188-27188/com.hj.test D/生命週期_Fragment2: onSaveInstanceState
09-20 22:00:30.528 27188-27188/com.hj.test D/生命週期_Fragment1: setUserVisibleHintfalse
09-20 22:00:30.528 27188-27188/com.hj.test D/生命週期_Fragment0: setUserVisibleHinttrue
09-20 22:00:30.530 27188-27188/com.hj.test D/生命週期_Fragment2: onPause
09-20 22:00:30.531 27188-27188/com.hj.test D/生命週期_Fragment2: onStop
09-20 22:00:30.531 27188-27188/com.hj.test D/生命週期_Fragment2: onDestroyView
09-20 22:00:30.533 27188-27188/com.hj.test D/生命週期_Fragment2: onDestroy
09-20 22:00:30.533 27188-27188/com.hj.test D/生命週期_Fragment2: onDetach
offscreenPageLimit為預設值1,預設載入當前介面左右各一個;左邊沒有,所以進入介面會載入第0個位置和預載入第1個位置的fragment;首先介面顯示前,兩個fragment的setUserVisibleHint都為false,不可見,然後對第0、1個位置進行初始化操作;當第0個位置的fragment的onAttach、onCreate執行後,會發現setUserVisibleHint為true了,我們會在這時候做一些資料載入的操作
LazyFragment程式碼如下:
package com.hj.test;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
public abstract class LazyFragment extends Fragment {
protected FragmentActivity mActivity;
protected boolean isVisibleToUser;//當前介面是否可見
protected boolean isViewInitialized;//view是否初始化完成
protected boolean isDataInitialized;//資料是否載入過
protected Bundle bundle;
public abstract void setUpView(View view);
public abstract void setUpData();
@Override
public void onAttach(Context context) {
super.onAttach(context);
bundle = getArguments();
Log.d("生命週期_Fragment"+bundle.get("id"),"onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpView(view);
isViewInitialized = true;
if (savedInstanceState != null){
onRestoreInstanceState(savedInstanceState);
}
if (isDataInitialized){
setUpData();
}else {
checkIsLoadData();//因為剛進來有setUserVisibleHint先執行,當前可見,但是view還沒有初始化的情況,所以在view初始化完畢還需要重新判斷是否需要懶載入資料
}
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
isDataInitialized = true;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
this.isVisibleToUser = isVisibleToUser;
checkIsLoadData();
Log.d("生命週期_Fragment"+getArguments().get("id"),"setUserVisibleHint"+isVisibleToUser);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("生命週期_Fragment"+bundle.get("id"),"onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d("生命週期_Fragment"+bundle.get("id"),"onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d("生命週期_Fragment"+bundle.get("id"),"onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d("生命週期_Fragment"+bundle.get("id"),"onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d("生命週期_Fragment"+bundle.get("id"),"onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
isViewInitialized = false;
Log.d("生命週期_Fragment"+bundle.get("id"),"onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("生命週期_Fragment"+bundle.get("id"),"onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d("生命週期_Fragment"+bundle.get("id"),"onDetach");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("生命週期_Fragment"+bundle.get("id"),"onSaveInstanceState");
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
Log.d("生命週期_Fragment"+bundle.get("id"),"onViewStateRestored");
}
/**
* 檢查是否需要載入資料
*/
private void checkIsLoadData(){
if(isVisibleToUser && isViewInitialized && !isDataInitialized){//當前可見&&view已經初始化&&資料沒有初始化
isDataInitialized = true;
setUpData();
}
}
}
SimpleFragment程式碼如下:
package com.hj.test;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Random;
public class SimpleFragment extends LazyFragment {
private TextView tv;
private String data;
@Override
public void setUpView(View view) {
tv = (TextView) view.findViewById(R.id.tv);
}
@Override
public void setUpData() {
Log.d("生命週期_Fragment"+bundle.get("id"),"lazyLoad第"+bundle.get("id")+"個介面準備載入資料");
if(TextUtils.isEmpty(data)){
data = new Random().nextInt(10)+"";
}
tv.setText(data);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("生命週期_Fragment"+bundle.get("id"),"onCreateView");
return inflater.inflate(R.layout.fragment_layout, null);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(!TextUtils.isEmpty(tv.getText())){
outState.putString("data",tv.getText().toString());
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
data = savedInstanceState.getString("data");
}
}
}
MainActivity程式碼如下:
package com.hj.test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.flyco.tablayout.SlidingTabLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final String[] mTitles = {"第0個", "第1個", "第2個", "第3個", "第4個"};
private ViewPager viewpager;
private MyAdapter adapter;
private SlidingTabLayout slidingtablayout;
private List<Fragment> fragments = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.exam_viewpager);
addFragments();
adapter = new MyAdapter(getSupportFragmentManager(), fragments);
viewpager.setAdapter(adapter);
slidingtablayout = (SlidingTabLayout) findViewById(R.id.examtablayout);
slidingtablayout.setViewPager(viewpager);
}
private void addFragments() {
for (int i=0;i<5;i++){
fragments.add(new SimpleFragment());
}
}
class MyAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
public MyAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = fragmentList.get(position);
Bundle bundle = new Bundle();
bundle.putInt("id",position);
fragment.setArguments(bundle);
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles[position];
}
}
}
activity_main.xml程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tl="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hj.test.MainActivity">
<com.flyco.tablayout.SlidingTabLayout
android:id="@+id/examtablayout"
android:layout_width="match_parent"
android:layout_height="50dp"
tl:tl_divider_width="0dp"
tl:tl_indicator_color="@android:color/holo_blue_light"
tl:tl_indicator_height="2dp"
tl:tl_tab_padding="20dp"
tl:tl_tab_space_equal="true"
tl:tl_textSelectColor="@android:color/background_dark"
tl:tl_textUnselectColor="@android:color/holo_red_light"
tl:tl_underline_color="@android:color/holo_blue_dark"
tl:tl_underline_height="1dp"
>
</com.flyco.tablayout.SlidingTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/exam_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/examtablayout"/>
</LinearLayout>
fragment_layout.xml程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.hj.test.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="fragment"/>
</LinearLayout>
有問題請留言指出,謝謝