Fragment之一:Fragment入门
生活随笔
收集整理的這篇文章主要介紹了
Fragment之一:Fragment入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考自張澤華視頻
Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展現不同的內容。
Fragment必須被嵌入Activity中使用,總是作為Activity的組成部分。
簡單示例:
一個Activity的界面由2個部分組成,每個部分分別是一個Fragment。
效果圖如下:
1、創建第一個Fragment的界面文件。
Fragment的界面文件和一般的Activity布局文件一樣,如。
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的類文件。
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入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Github android客户端源代码
- 下一篇: Fragment之三:根据屏幕尺寸加载不