日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Fragment之一:Fragment入门

發布時間:2024/1/23 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fragment之一:Fragment入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考自張澤華視頻


Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展現不同的內容。

Fragment必須被嵌入Activity中使用,總是作為Activity的組成部分。


簡單示例:

一個Activity的界面由2個部分組成,每個部分分別是一個Fragment。

效果圖如下:



1、創建第一個Fragment的界面文件。

Fragment的界面文件和一般的Activity布局文件一樣,如。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#0000ff"android:orientation="vertical" ><TextView?android:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/fragment1"/></LinearLayout>

2、創建第一個Fragment的類文件。

package com.ljh.fragmentdemo;import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;//繼續Fragment類。 public class Fragment1 extends Fragment {@Override//在Fragment被加載時調用public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加載某個布局文件。return inflater.inflate(R.layout.fragment1, null);} }

3、創建第二個Fragment的界面文件。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#00ff00"android:orientation="vertical" ><TextView android:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/fragment2"/></LinearLayout>

4、創建第二個Fragment的類文件。

package com.ljh.fragmentdemo;import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;//繼續Fragment類。 public class Fragment2 extends Fragment {@Override//在Fragment被加載時調用public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加載某個布局文件。return inflater.inflate(R.layout.fragment2, null);}}
5、創建主界面文件。

<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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><fragmentandroid:id="@+id/fragment1"android:name="com.ljh.fragmentdemo.Fragment1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><fragmentandroid:id="@+id/fragment2"android:name="com.ljh.fragmentdemo.Fragment2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout>

6、創建主類。

本步驟的關鍵是用Fragment對象取代布局文件中的內容。

package com.ljh.fragmentdemo;import com.ljh.fragmentdemo.R;import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//獲得FragmentManager,然后獲取FragmentTransaction。FragmentManager fm = getFragmentManager();FragmentTransaction transaction = fm.beginTransaction();//用Fragment動態代替布局文件中的內容transaction.replace(R.id.fragment1, new Fragment1());transaction.replace(R.id.fragment2, new Fragment2());//提交事務transaction.commit();}}

注:

1、對兩個Fragment的分別進行編輯,如

@Override//在Fragment被加載時調用public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加載某個布局文件。View root = inflater.inflate(R.layout.fragment1, null);TextView tvContent = (TextView) root.findViewById(R.id.tv_content);tvContent.setText("hello");return root;}
2、其它內容見后面博客:

(1)Android2.3及以前對Fragment的支持。

(2)使用Fragment使不同的尺寸的屏幕展現不同內容。

(3)Activity與Fragment之間的通信。


創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Fragment之一:Fragment入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。