最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android app開發(fā)中的Fragment入門學(xué)習(xí)教程

 更新時(shí)間:2016年02月25日 16:33:24   作者:yanfeichening  
這篇文章主要介紹了Android app開發(fā)中的Fragment入門學(xué)習(xí)教程,包括Fragment的創(chuàng)建和XML布局文件中的Fragment定義等,需要的朋友可以參考下

在Android3.0上開始引入了一個(gè)新概念叫Fragment。它有自己的布局文件,可以作為組件排布,也可以相互組合去實(shí)現(xiàn)不同的布局顯示。使用Fragment可以重復(fù)利用代碼,并且可以滿足不同設(shè)備尺寸的需求。Fragment不能單獨(dú)存在,只能存在于Activity中,而一個(gè)Activity可以擁有多個(gè)Fragment。很重要的一點(diǎn)是,F(xiàn)ragment可以和Activity中的其它組件一起使用,無(wú)需重寫所有Activity的接口。所以使用Fragment就可以這樣來(lái)完成上例中“主界面—詳細(xì)界面”的APP需求。

在手機(jī)上是這樣顯示的:

2016225161854439.png (300×192)

而在平板上是這樣的:

2016225161915086.png (300×177)

在一個(gè)小屏幕的設(shè)備上,一個(gè)activity通常占據(jù)了整個(gè)屏幕,同時(shí)顯示各種UI視圖組件。Activity實(shí)際上就是視圖的容器。然后,當(dāng)一個(gè)activity被顯示在一個(gè)大屏幕的設(shè)備上,例如平板電腦,總會(huì)顯得有些不適應(yīng)。因?yàn)槠聊惶罅?,activity中的所有UI組件要充滿整個(gè)屏幕,這樣一來(lái),視圖的層次結(jié)構(gòu)就很復(fù)雜了。一個(gè)更好的辦法是使用一種“輕量級(jí)”的activity,每個(gè)“輕量級(jí)”activity包含自己的視圖,互不干擾。在運(yùn)行期間,根據(jù)屏幕的方向和尺寸,一個(gè)activity可以包含一個(gè)或多個(gè)“輕量級(jí)”activity。在Android3.0以上的版本,這種“輕量級(jí)”的activity叫做Fragment.

怎么創(chuàng)建一個(gè)Fragment

現(xiàn)在我們了解了Fragment的生命周期了,接著我們就需要知道怎么創(chuàng)建一個(gè)Fragment并綁定到Activity中,第一件要做的事就是繼承android.app.Fragment來(lái)寫一個(gè)Fragment,假設(shè)我們的Fragment叫做Fragment1,創(chuàng)建和定義如下:

public class Fragment1 extends Fragment {
...
}

就像我們上面說(shuō)的,F(xiàn)ragment只能存在于Activity中,所以我們必須要在某處定義它,有兩種方式:
- 直接在xml布局文件中定義;
- 在xml布局文件中定義一個(gè)占位符,然后動(dòng)態(tài)地在Activity中操作Fragment;

我們定義Fragment的方式會(huì)影響它的生命周期,因?yàn)樵谏鲜龅谝环N情況下onInflate方法會(huì)被調(diào)用,而第二種情況下它的生命周期是從onAttach方法開始的。

如果我們?cè)赬ML文件中定義Fragment的話,我們需要:

<fragment android:id="@+id/f1"
            class="com.survivingwithandroid.fragment.Fragment1"
       android:layout_width="match_parent"
       android:layout_height="20dp"/>

然而如果我們?cè)赬ML中用占位符的話,需要再做一些工作。

布局框架和Fragment

如果我們?cè)赬ML布局文件中定義Fragment的話,就不能自由、動(dòng)態(tài)修改Fragment了,還有別的方法可以讓我們可以更靈活地操作:使用時(shí)需要在XML文件中定義:

<FrameLayout android:id="@+id/fl1"
       android:layout_width="match_parent"
       android:layout_height="200dp"/>

在Activity里面還需要做一點(diǎn)工作,因?yàn)槲覀儽仨毷謩?dòng)初始化Fragment,然后把它“插入”到FrameLayout中。

public class MainActivity extends Activity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  Fragment2 f2 = new Fragment2();
  FragmentTransaction ft = getFragmentManager().beginTransaction();
  ft.replace(R.id.fl1, f2);
  ft.commit();
}


例子

可以把Fragment想象成Activity的另外一種形式。你創(chuàng)建fragments去包含UI組件,就像創(chuàng)建activities那樣。但是,F(xiàn)ragment總是被嵌在Activity中。

下面來(lái)通過(guò)一個(gè)例子看一下流程:

1.創(chuàng)建一個(gè)名為Fragments的工程。
2.在res/layout文件夾下,新建一個(gè)叫fragment1.xml的文件。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:background="#00FF00" 
  android:orientation="vertical" > 
 
  <TextView 
    android:id="@+id/lblFragment1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is fragment #1" 
    android:textColor="#000000" 
    android:textSize="25sp" /> 
 
</LinearLayout> 

3.在res/layout文件夾下,新建一個(gè)叫fragment2.xml的文件。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:background="#FFFE00" 
  android:orientation="vertical" > 
 
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is fragment #2" 
    android:textColor="#000000" 
    android:textSize="25sp" /> 
 
  <Button 
    android:id="@+id/btnGetText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="onClick" 
    android:text="Get text in Fragment #1" 
    android:textColor="#000000" /> 
 
</LinearLayout> 

4.main.xml中的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="horizontal" > 
 
  <fragment 
    android:id="@+id/fragment1" 
    android:name="net.learn2develop.Fragments.Fragment1" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 
 
  <fragment 
    android:id="@+id/fragment2" 
    android:name="net.learn2develop.Fragments.Fragment2" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 
 
</LinearLayout> 

5.新建兩個(gè)類:Fragment1.java和Fragment2.java。
6.Fragment1.java中的代碼。

package net.learn2develop.Fragments; 
 
import android.app.Activity; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
public class Fragment1 extends Fragment { 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
 
    Log.d("Fragment 1", "onCreateView"); 
 
    // ---Inflate the layout for this fragment--- 
    return inflater.inflate(R.layout.fragment1, container, false); 
  } 
} 

7.Fragment2.java中的代碼。

package net.learn2develop.Fragments; 
 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class Fragment2 extends Fragment { 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
    // ---Inflate the layout for this fragment--- 
    return inflater.inflate(R.layout.fragment2, container, false); 
  } 
} 

相關(guān)文章

  • Android性能優(yōu)化之圖片大小,尺寸壓縮綜合解決方案

    Android性能優(yōu)化之圖片大小,尺寸壓縮綜合解決方案

    隨著Android手機(jī)的越來(lái)越先進(jìn),給我們開發(fā)者而言傳遞的圖片也是越來(lái)越大,這個(gè)時(shí)候我們可以對(duì)一些沒(méi)有必要原圖展示的圖片進(jìn)行壓縮,這篇文章主要給大家介紹了關(guān)于Android性能優(yōu)化之圖片大小,尺寸壓縮的綜合解決方案,需要的朋友可以參考下
    2022-04-04
  • Android逆向入門之常見Davlik字節(jié)碼解析

    Android逆向入門之常見Davlik字節(jié)碼解析

    Dalvik是Google公司自己設(shè)計(jì)用于Android平臺(tái)的虛擬機(jī)。Dalvik虛擬機(jī)是Google等廠商合作開發(fā)的Android移動(dòng)設(shè)備平臺(tái)的核心組成部分之一,本篇文章我們來(lái)詳細(xì)解釋常見Davlik字節(jié)碼
    2021-11-11
  • Flutter路由之fluro的配置及跳轉(zhuǎn)

    Flutter路由之fluro的配置及跳轉(zhuǎn)

    本文主要介紹了Flutter路由之fluro的配置及跳轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 詳解Android 在 ViewPager 中使用 Fragment 的懶加載

    詳解Android 在 ViewPager 中使用 Fragment 的懶加載

    本篇文章主要介紹了Android 在 ViewPager 中使用 Fragment 的懶加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • android自定義控件ImageView實(shí)現(xiàn)圓形圖片

    android自定義控件ImageView實(shí)現(xiàn)圓形圖片

    這篇文章主要為大家詳細(xì)介紹了android自定義控件ImageView實(shí)現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android實(shí)現(xiàn)圖片輪播列表

    Android實(shí)現(xiàn)圖片輪播列表

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片輪播列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android?Jetpack?組件LiveData源碼解析

    Android?Jetpack?組件LiveData源碼解析

    這篇文章主要為大家介紹了Android?Jetpack?組件LiveData源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android開發(fā)apk反編譯和二次打包教程

    Android開發(fā)apk反編譯和二次打包教程

    反編譯不是讓各位開發(fā)者去對(duì)一個(gè)應(yīng)用破解搞重裝什么的,主要目的是為了促進(jìn)開發(fā)者學(xué)習(xí),借鑒好的代碼,提升自我開發(fā)水平。下面我們就來(lái)研究下如何進(jìn)行APK反編譯以及二次打包
    2016-04-04
  • Android面試筆記之常問(wèn)的Context

    Android面試筆記之常問(wèn)的Context

    Android技術(shù)面試確實(shí)常常被問(wèn)到Context,大概問(wèn)題就是說(shuō)說(shuō)你對(duì)Context的理解吧,當(dāng)時(shí)腦袋里浮現(xiàn)了是原來(lái)看到的文章片段亂說(shuō)一通,這樣還是不行的。平時(shí)還是多積累知識(shí),深刻理解Context,在項(xiàng)目開發(fā)過(guò)程中也能避免一些陷入坑中。下面就來(lái)看看個(gè)人的一些總結(jié)吧。
    2016-12-12
  • Android仿制淘寶滾動(dòng)圖文條的示例代碼

    Android仿制淘寶滾動(dòng)圖文條的示例代碼

    這篇文章主要介紹了Android仿制淘寶滾動(dòng)圖文條的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08

最新評(píng)論

呈贡县| 和田市| 芦溪县| 屯留县| 盐池县| 龙江县| 阿坝| 峡江县| 常宁市| 信阳市| 定边县| 海晏县| 长垣县| 固阳县| 海兴县| 文山县| 宁城县| 苗栗市| 宁陵县| 武山县| 中西区| 吉隆县| 来安县| 香格里拉县| 襄城县| 景东| 攀枝花市| 金平| 建湖县| 商洛市| 聊城市| 祥云县| 建瓯市| 万源市| 甘孜县| 五家渠市| 绥宁县| 准格尔旗| 莒南县| 白城市| 东乌|