目錄
一、創建一個Fragment專案
二、在專案中創建一個左碎片布局
三、創建一個類來控制布局
四、創建另一個右碎片布局
五、創建布局對應的類
六、創建第二個右布局
七、創建對應的類
八、更改主布局
九、在主活動中注冊功能
Fragmen 主要用于平板電腦或手機橫屏
一、創建專案
創建一個新專案專案名為Fragment


二、創建第一個碎片布局
在layout下創建一個布局
右擊 layout / New / Layout Resource File
取名為 left_frag 并使用 LinearLayout 布局

左碎片布局設定成按鈕欄
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_1"
android:text="碎片1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_2"
android:text="碎片2"/>
</LinearLayout>
三、創建一個類來控制布局
在 java / 右擊 com.example.fragment / New / Java Class
類名取為 LeftFrag

LeftFrag 類中的代碼如下
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class LeftFrag extends Fragment {
/**
* Called to have the fragment instantiate its user interface view.
* This is optional, and non-graphical fragments can return null. This will be called between
* {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
* <p>A default View can be returned by calling in your
* constructor. Otherwise, this method returns null.
*
* <p>It is recommended to <strong>only</strong> inflate the layout in this method and move
* logic that operates on the returned View to {@link #onViewCreated(View, Bundle)}.
*
* <p>If you return a View from here, you will later be called in
* {@link #onDestroyView} when the view is being released.
*
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed
* from a previous saved state as given here.
* @return Return the View for the fragment's UI, or null.
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_frag,container,false);
return view;
}
}
四、創建第一個右布局
同樣的方法創建一個右布局
取名為:righr_frag1
右布局設定成顯示欄
righr_frag1 中的代碼為:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffccff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="第一個碎片內容"/>
</LinearLayout>
五、創建右碎片布局對應的類
同樣的方法創建一個類
取名為:RightFrag1

RightFrag1類中的代碼為:
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class RightFrag1 extends Fragment {
/**
* Called to have the fragment instantiate its user interface view.
* This is optional, and non-graphical fragments can return null. This will be called between
* {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
* <p>A default View can be returned by calling in your
* constructor. Otherwise, this method returns null.
*
* <p>It is recommended to <strong>only</strong> inflate the layout in this method and move
* logic that operates on the returned View to {@link #onViewCreated(View, Bundle)}.
*
* <p>If you return a View from here, you will later be called in
* {@link #onDestroyView} when the view is being released.
*
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed
* from a previous saved state as given here.
* @return Return the View for the fragment's UI, or null.
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_frag1,container,false);
return view;
}
}
六、創建第二個右布局
同樣的方法創建第二個右布局
取名為:
right_frag2

right_frag2 中的代碼為:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#550099">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="第二個碎片內容"/>
</LinearLayout>
七、創建第二個右布局對應的類
同樣的方法
想必你已經猜到它的名稱了
沒錯就是:RightFrag2

RightFrag2 中的代碼為:
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class RightFrag2 extends Fragment {
/**
* Called to have the fragment instantiate its user interface view.
* This is optional, and non-graphical fragments can return null. This will be called between
* {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
* <p>A default View can be returned by calling in your
* constructor. Otherwise, this method returns null.
*
* <p>It is recommended to <strong>only</strong> inflate the layout in this method and move
* logic that operates on the returned View to {@link #onViewCreated(View, Bundle)}.
*
* <p>If you return a View from here, you will later be called in
* {@link #onDestroyView} when the view is being released.
*
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed
* from a previous saved state as given here.
* @return Return the View for the fragment's UI, or null.
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_frag2,container,false);
return view;
}
}
八、修改主布局
把activity_main 中的代碼改為:
<?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"
android:orientation="horizontal">
//左布局設定為靜態
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/left_layout"
android:name="com.example.fragment.LeftFrag"/>
//右布局設定為動態
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:id="@+id/right_layout"/>
</LinearLayout>
九、在主活動中注冊功能
MainActivity 中的代碼為:
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_1 = (Button) findViewById(R.id.btn_1);
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new RightFrag1());
}
});
Button btn_2 = (Button) findViewById(R.id.btn_2);
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new RightFrag2());
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}
到這里你對Fragment應該有個印象了試著自己做一個新聞頁面吧
謝謝閱讀,你的關注是我創作的最大動力😘
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/355792.html
標籤:其他
上一篇:檢查手機是否安裝某一app
下一篇:使用復雜型別引數強制記錄的問題
