日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > 互聯(lián)網(wǎng) > 【Android開發(fā)】之Fragment開發(fā)1

【Android開發(fā)】之Fragment開發(fā)1

來源:程序員人生   發(fā)布時間:2014-09-02 22:03:41 閱讀次數(shù):3646次

 

    一直知道Fragment很強(qiáng)大,但是一直都沒有去學(xué)習(xí),現(xiàn)在有些空閑的時間,所以就去學(xué)習(xí)了一下Fragment的簡單入門。我也會把自己的學(xué)習(xí)過程寫下來,如果有什么不足的地方希望大牛指正,共同進(jìn)步!

 

  一、Fragment簡介

    1.Fragment作為Activity界面的一部分組成出現(xiàn);

    2.可以在一個Activity中同時出現(xiàn)多個Fragment,并且,一個Fragment亦可在多個Activity中使用;

    3.在Activity運行過程中,可以添加、移除或者替換Fragment(add()、remove()、replace());

    4.Fragment可以響應(yīng)自己的輸入事件,并且有自己的生命周期,當(dāng)然,它們的生命周期直接被其所屬的activity的生命周期影響。

    那我們?yōu)槭裁匆肍ragment呢?主要目的是用在大屏幕設(shè)備上--例如平板電腦上,支持更加動態(tài)和靈活的UI設(shè)計。平板電腦的屏幕要比手機(jī)的大得多,有更多的空間來放更多的UI組件,并且這些組件之間會產(chǎn)生更多的交互。我們可以把Fragment認(rèn)為是“小的Activity”,F(xiàn)ragment更加簡潔。

 

 

  二、Fragment的簡單使用

    那我們就簡單的顯示2個Fragment為例來講解一下。

    

    1.在XML中添加Fragment:

      新建Fragment1、Fragment2(注意:這里可能有2個包可以選擇導(dǎo)入android.app.Fragment或android.support.v4.app.Fragment都是可以的,我這里選擇使用了前者,但是兩者使用時有區(qū)別的,在結(jié)尾中我會講到):


      Fragment1代碼:
package com.example.fragment; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e("TAG", "in"); return inflater.inflate(R.layout.fragment1, container, false); } }

      Fragment2代碼:
package com.example.fragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } }

      Fragment1的xml代碼:
<?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="#FF69B4" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="這是第一個Fragment" /> </LinearLayout>

      Fragment2的xml代碼:
<?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="#EECBAD" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="這是第二個Fragment" /> </LinearLayout>

      我們在activity_main.xml中添加兩個Fragment,代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" > <fragment android:id="@+id/fragment1" android:name="com.example.fragment.Fragment1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="com.example.fragment.Fragment2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>

      MainActivity代碼如下:
package com.example.fragmentdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }

      然后運行工程就可以顯示Fragment了,下面是效果圖。

    

       

    2.動態(tài)添加Fragment:

      我們只需要修改MainActivity和activity_main.xml中的代碼就可以了。

      activity_main.xml代碼:                 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical" > <LinearLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> </LinearLayout>

       MainActivity代碼:
package com.example.fragmentdemo; import android.app.Activity; import android.os.Bundle; import com.example.fragment.Fragment1; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getFragmentManager().beginTransaction() .replace(R.id.main, new Fragment1()).commit(); } }

      然后運行工程就可以動態(tài)的顯示Fragment了,下面是效果圖。

 

三、app包下和V4包下的Fragment的區(qū)別

1、盡量不要用app包中的fragment,因為這個是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的;

    2、android.support.v4.app.Fragment:可以兼容到1.6的版本

    3、關(guān)于這兩個fragment使用<fragment>標(biāo)簽的問題:

    (1)app.fragment和v4.fragment都是可以使用<fragment>標(biāo)簽的只是在在使用的時候如果是app.fragment則沒有什么特殊的地方繼承Activity即可。

    (2)當(dāng)v4.fragment使用<fragment>標(biāo)簽的時候就要特別注意了:當(dāng)這個Activity的布局中有<fragment>標(biāo)簽的時候,這個Activity必須繼承FragmentActivity,否則就會報錯。此時如果不卜繼成FragmentActivity的話 編譯系統(tǒng)會把<fragment>認(rèn)為是app包中的Fragment來處理。但是此時我們導(dǎo)入的是v4包中的FragmentAndroid官方文檔中的Fragment的例子就是以app包中的Fragment來講解的。

            (3)app包中關(guān)于Fragment的類和方法在V4包中都是有相應(yīng)的對應(yīng)的。

    轉(zhuǎn)載自:http://blog.csdn.net/a465456465/article/details/10415211,感謝。



    上面就是Fragment的簡單使用方法,Demo下載,下一節(jié)我會講Fragment的詳細(xì)使用。歡迎關(guān)注,我的博客園地址:http://www.cnblogs.com/getherBlog/p/3943547.html。

 


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产一区二区视频网站 | 99午夜 | 欧美视频一区二区 | 成人性生活大片免费看一 | 夜夜骑狠狠干 | 精品国产99久久久久久宅男i | 青青草这里只有精品 | 婷婷综合五月 | 国产不卡视频在线观看 | 91久久国产综合久久91精品网站 | 精品久久久久久久久久久下田 | 99re在线视频 | 日韩精选 | 99精品视频免费观看 | av动漫一区 | 国产一区二区三区在线观看视频 | 国产91av在线 | 国产精品99久久久 | 日韩精品一区二区三区av | 久久99精品久久久久子伦 | 亚洲精品在线观看视频 | 一区二区在线免费 | 亚洲成年人网址 | 国产一区二区三区精彩视频 | 激情av在线播放 | 美日韩免费视频 | 日本中文字幕在线观看 | 日本不卡高清视频 | 亚洲四区 | 一区免费视频 | 国产网红女主播免费视频 | a级片在线免费看 | 国产成人午夜视频 | 可以在线看的av | 在线观看视频一区 | 欧美精品福利 | 日韩一区二区三区在线播放 | 热久久免费视频 | 国产高清无密码一区二区三区 | 男女超级黄aaa大片免费 | 成人黄色一级视频 |