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

Android中碎片的使用方法詳解

 更新時(shí)間:2019年06月20日 10:29:05   作者:Kepler  
這篇文章主要介紹了Android中碎片的使用方法詳解,其實(shí)碎片很簡(jiǎn)單,但是網(wǎng)上胡亂充數(shù)的博文太多了,以至于我們有時(shí)候覺(jué)得比較亂,今天就來(lái)簡(jiǎn)單講解一下碎片的使用,需要的朋友可以參考下

Fragment的使用

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

靜態(tài)添加碎片

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

兩個(gè)布局都很簡(jiǎn)單,里面只有一個(gè)居中顯示的TextView,下面貼一下代碼.

第一個(gè)布局文件:fragment_first.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="@android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the first fragment!"/>
</LinearLayout>

第二個(gè)布局文件fragment_second.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="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the second fragment!"/>
</LinearLayout>

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

貼一下兩個(gè)類的代碼,也很簡(jiǎn)單,只是重寫了onCreateView方法來(lái)加載不同的布局文件.

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

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

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

先貼一下第一個(gè)Activity的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns: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">
<fragment
android: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>
<fragment
android: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>
<Button
android:id="@+id/main_btnGoNext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Go to next page"
android:textAllCaps="false"/>
</LinearLayout>


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

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

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

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

動(dòng)態(tài)添加碎片

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

先貼一下第二個(gè)Activity的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns: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">
<Button
android:id="@+id/second_btnLoadFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load First!"
android:textAllCaps="false"/>
<Button
android:id="@+id/second_btnLoadSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Second!"
android:textAllCaps="false"/>
<FrameLayout
android:id="@+id/second_fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>

上面的兩個(gè)按鈕用來(lái)加載不同的碎片,而下面的FrameLayout就是碎片顯示的容器.

廢話不多說(shuō),貼代碼:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

//點(diǎn)擊第一個(gè)按鈕的時(shí)候加載第一個(gè)碎片
findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.second_fl, new FirstFragment());
transaction.commit();
}
});
//點(diǎn)擊第二個(gè)按鈕的時(shí)候加載第二個(gè)碎片
findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//簡(jiǎn)寫
}
});
}
}

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio下無(wú)線調(diào)試的方法

    Android Studio下無(wú)線調(diào)試的方法

    這篇文章主要為大家詳細(xì)介紹了Android Studio平臺(tái)下無(wú)線調(diào)試的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android Notification通知解析

    Android Notification通知解析

    這篇文章主要針對(duì)Android Notification通知進(jìn)行解析,本文主要介紹的是notification通知的使用方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android應(yīng)用內(nèi)存泄漏優(yōu)化指南

    Android應(yīng)用內(nèi)存泄漏優(yōu)化指南

    內(nèi)存泄漏(Memory Leak)是指應(yīng)用中不再使用的對(duì)象因錯(cuò)誤引用無(wú)法被垃圾回收(GC),導(dǎo)致內(nèi)存占用持續(xù)增長(zhǎng),最終可能引發(fā) OOM(Out Of Memory)崩潰?或 應(yīng)用卡頓,以下是 Android 內(nèi)存泄漏的優(yōu)化方案,涵蓋檢測(cè)工具、常見(jiàn)場(chǎng)景及解決方案,需要的朋友可以參考下
    2025-03-03
  • Android編寫2048小游戲

    Android編寫2048小游戲

    這篇文章主要為大家詳細(xì)介紹了利用Android編寫一個(gè)2048小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Flutter軟鍵盤的原理淺析

    Flutter軟鍵盤的原理淺析

    大家應(yīng)該都知道目前Flutter官方是沒(méi)有自定義鍵盤的解決方案,下面這篇文章主要給大家介紹了關(guān)于Flutter軟鍵盤原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例

    Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例

    本篇文章主要介紹了Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • Android實(shí)現(xiàn)下拉放大圖片松手自動(dòng)反彈效果

    Android實(shí)現(xiàn)下拉放大圖片松手自動(dòng)反彈效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)下拉放大圖片松手自動(dòng)反彈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Android快速分析apk工具aapt的使用教程

    Android快速分析apk工具aapt的使用教程

    這篇文章主要介紹了Android快速分析apk工具aapt的使用教程,本文講解了什么是aapt、主要用法、使用aapt、查看apk的基本信息、查看基本信息、查看應(yīng)用權(quán)限等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Android如何防止apk程序被反編譯(尊重勞動(dòng)成果)

    Android如何防止apk程序被反編譯(尊重勞動(dòng)成果)

    作為Android應(yīng)用開(kāi)發(fā)者,不得不面對(duì)一個(gè)尷尬的局面,就是自己辛辛苦苦開(kāi)發(fā)的應(yīng)用可以被別人很輕易的就反編譯出來(lái),天下痛苦之事莫過(guò)于此啊,本文會(huì)介紹一種防止apk程序被反編譯的方法,感興趣的朋友可以了解下哦
    2013-01-01
  • Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡

    Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評(píng)論

慈利县| 化德县| 伽师县| 页游| 贡山| 北川| 治多县| 石狮市| 高台县| 红原县| 平定县| 湖州市| 汉阴县| 德庆县| 时尚| 阳原县| 邯郸市| 凤阳县| 朔州市| 巴东县| 龙井市| 印江| 平和县| 隆安县| 白玉县| 镇雄县| 恭城| 安溪县| 卢湾区| 安泽县| 新昌县| 怀柔区| 桃园县| 广水市| 厦门市| 兴隆县| 洛阳市| 邻水| 遵义县| 航空| 林西县|