欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Android中碎片的使用方法詳細(xì)介紹

本篇內(nèi)容主要講解“Android中碎片的使用方法詳細(xì)介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android中碎片的使用方法詳細(xì)介紹”吧!

“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無止境。

Fragment的使用

其實(shí)碎片很簡單,但是網(wǎng)上胡亂充數(shù)的博文太多了,以至于我們有時候覺得比較亂,今天就來簡單講解一下碎片的使用.碎片的使用分為兩種,靜態(tài)添加碎片和動態(tài)添加碎片,我們就先來看一下靜態(tài)添加碎片如何實(shí)現(xiàn).

靜態(tài)添加碎片

首先,先建兩個Layout文件,這就是碎片的布局文件,大家可能也發(fā)現(xiàn)了,Android Studio里面可以直接快速建立碎片,就像Activity一樣,但是這樣會生成很多沒用的代碼,所以我們還是選擇自己創(chuàng)建碎片布局.

兩個布局都很簡單,里面只有一個居中顯示的TextView,下面貼一下代碼.

第一個布局文件:fragment_first.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/holo_blue_light"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="This is the first fragment!"/></LinearLayout>

第二個布局文件fragment_second.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/holo_green_light"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="This is the second fragment!"/></LinearLayout>

現(xiàn)在布局文件建完了,我們該建立他們對應(yīng)的Fragment了,也就是后臺代碼了。新建兩個類,分別叫FirstFragment和SecondFragment,都繼承于Fragment,需要注意一點(diǎn),我們教程里面所使用的Fragment全都是android.support.v4.app.Fragment這個包下的,這樣更有利于程序的兼容性.

貼一下兩個類的代碼,也很簡單,只是重寫了onCreateView方法來加載不同的布局文件.

public class FirstFragment extends Fragment {private View view;//得到碎片對應(yīng)的布局文件,方便后續(xù)使用//記住一定要重寫onCreateView方法@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_first, container, false);//得到對應(yīng)的布局文件return view;}}public class SecondFragment extends Fragment {private View view;//得到碎片對應(yīng)的布局文件,方便后續(xù)使用//記住一定要重寫onCreateView方法@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_second, container, false);//得到對應(yīng)的布局文件return view;}}

好,基本的工作我們做完了,現(xiàn)在我們用兩個Activity展示如何靜態(tài)添加碎片和動態(tài)添加碎片.

靜態(tài)添加控件的話,需要使用fragment控件,指定其名稱是你剛才創(chuàng)建的Fragment就可以,讓我們來看一下.

先貼一下第一個Activity的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="me.worldskills.android.testfragment.MainActivity"><fragmentandroid:id="@+id/main_firstfragment"android:name="me.worldskills.android.testfragment.FirstFragment"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></fragment><fragmentandroid:id="@+id/main_secondfragment"android:name="me.worldskills.android.testfragment.SecondFragment"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></fragment><Buttonandroid:id="@+id/main_btnGoNext"android:layout_width="match_parent"android:layout_height="50dp"android:text="Go to next page"android:textAllCaps="false"/></LinearLayout>

其中那個按鈕是用來跳轉(zhuǎn)到下一個界面的,也就是動態(tài)添加碎片案例的Activity,在這里可以忽略.

這里我們看見了,兩個fragment分別指定name為FirstFragment和SecondFragment,也就是你剛才創(chuàng)建的兩個Fragment,一定要記得加上包名.對了,還有一個問題,就是這樣的話是沒有預(yù)覽的,如果想要預(yù)覽,需要在fragment標(biāo)簽中加上一句代碼:

Tools:layout="@layout/布局文件名稱" .

好了,靜態(tài)添加碎片就完成了,什么?就這么簡單,對啊...就這么簡單.

動態(tài)添加碎片

動態(tài)添加碎片我們就不需要用fragment控件了,而是需要用個FrameLayout控件,這是為什么呢,首先我們都知道FrameLayout中的控件,都是從左上角開始顯示,不用進(jìn)行位置控制,動態(tài)添加碎片其實(shí)就是向容器里面動態(tài)添加碎片,而fragment控件只能用來靜態(tài)綁定一個碎片.

先貼一下第二個Activity的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_second"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/second_btnLoadFirst"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Load First!"android:textAllCaps="false"/><Buttonandroid:id="@+id/second_btnLoadSecond"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Load Second!"android:textAllCaps="false"/><FrameLayoutandroid:id="@+id/second_fl"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout></LinearLayout>

上面的兩個按鈕用來加載不同的碎片,而下面的FrameLayout就是碎片顯示的容器.

廢話不多說,貼代碼:

import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;public class SecondActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//點(diǎn)擊第一個按鈕的時候加載第一個碎片findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.second_fl, new FirstFragment());transaction.commit();}});//點(diǎn)擊第二個按鈕的時候加載第二個碎片findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//簡寫}});}}

getSupportFragmentManager方法用來獲得一個碎片管理器對象(使用這個方法的時候注意是android.support.v4.app包下的哦),然后通過這個方法開始一個碎片事物對象,這個對象比較關(guān)鍵,可以用來動態(tài)添加碎片,調(diào)用它的replace方法,會把指定容器里面的其他控件全部清除掉,然后添加新的碎片進(jìn)去.在這里就是先把R.id.second_f1里面的控件清空,然后添加傳入一個FirstFragment進(jìn)去.

替換完之后一定要記得調(diào)用commit方法提交,要不然你的所有操作都不會生效,切記.

到此,相信大家對“Android中碎片的使用方法詳細(xì)介紹”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

新聞標(biāo)題:Android中碎片的使用方法詳細(xì)介紹
文章URL:http://aaarwkj.com/article22/jeghcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、虛擬主機(jī)、電子商務(wù)、小程序開發(fā)品牌網(wǎng)站設(shè)計、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
久久三级中文欧大战字幕| 少妇午夜福利一区二区| 国产极品嫩模在线观看91| 日韩亚洲av一区二区| 做性视频大全在线观看| 国产日本欧美一区二区三区| 日韩精品有码在线视频免费观看| 日韩欧美日日夜夜精品| 久久99热最新地址获取| 人成在线免费视频网站| 视频一区中文字幕在线| 亚洲精品一区二区三区小| 欧美丰满老妇性猛交| 精品中文字幕欧美区一区| 观看女性真实高潮的合集| 亚洲一区二区天堂av| 欧美激情欧美精品欧美色浮| 高清不卡日本一区二区三区久久| 日本激情精品在线观看| 久草手机福利在线观看| 天天操操操操操操夜夜爽| 国产成人色污在线观看| 欧美精品久久91九色| 亚洲国产女人精品久久久| 国产成人精品一区二区国产乱码| 黄片在线免费在线播放| 九九视频在线观看免费观看| 亚洲中文字幕乱码丝袜在线精品| 天堂av免费资源在线观看| 激情自拍偷拍合集一部| 国产日韩精品一区二区在线| 日本高清不卡在线一区二区| 日本一区二区三区日韩欧美 | 三级视频一区二区三区| 日本精品在线不卡视频| 日韩夫妻精品熟妇人妻一区| 日韩在线不卡免费视频一区| 激情五月婷婷我有我色| 欧美在线日韩一区二区| 国产成人久久精品二区三区| 欧美三级精品三级在线|