日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android学习笔记之Fragment的两种使用方法

發布時間:2025/6/15 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习笔记之Fragment的两种使用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、第一種方法:

(1)Fragment的第一種使用方法是使用fragment加載單獨的布局文件:(也就是xml的方式實現)

結構如下:


activity_main.xml主要是在一個線性布局中添加兩個線性布局

[html]?view plaincopy
  • <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:orientation="horizontal"??
  • ????tools:context=".MainActivity"?>??
  • ??
  • ????<LinearLayout??
  • ????????android:id="@+id/linerlayout1"??
  • ????????android:layout_width="0dp"??
  • ????????android:layout_height="match_parent"??
  • ????????android:layout_weight="1"??
  • ????????android:background="#CCCCCC"??
  • ????????android:orientation="vertical"?>??
  • ??
  • ????????<Button??
  • ????????????android:id="@+id/button1"??
  • ????????????android:layout_width="match_parent"??
  • ????????????android:layout_height="wrap_content"??
  • ????????????android:text="顯示窗口"?/>??
  • ????</LinearLayout>??
  • ??
  • ????<LinearLayout??
  • ????????android:id="@+id/linerlayout2"??
  • ????????android:layout_width="0dp"??
  • ????????android:layout_height="match_parent"??
  • ????????android:layout_weight="3"??
  • ????????android:background="#CCFFDD"??
  • ????????android:orientation="vertical"?>??
  • ????</LinearLayout>??
  • ??
  • </LinearLayout>??

  • right.xml是等會使用fragment的時候,加載的一個布局文件:(由于主要是在界面中加載、所以不作特殊要求)

    [html]?view plaincopy
  • <?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:orientation="vertical"?>??
  • ??
  • ????<RatingBar??
  • ????????android:id="@+id/ratingBar1"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"?/>??
  • ??
  • ????<Button??
  • ????????android:id="@+id/button11"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="點我試試"?/>??
  • ??
  • </LinearLayout>??

  • MyFragment.java就是加載fragment的類,要繼承Fragment類:(要重載父類的下邊三個方法)

    [java]?view plaincopy
  • package?com.lc.tablet_fragment_addview;??
  • ??
  • import?android.app.Fragment;??
  • import?android.os.Bundle;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.view.ViewGroup;??
  • import?android.widget.Button;??
  • import?android.widget.Toast;??
  • ??
  • public?class?MyFragment?extends?Fragment?{??
  • ??
  • ????public?MyFragment()?{??
  • ????????//?TODO?Auto-generated?constructor?stub??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onCreate(savedInstanceState);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??
  • ????????????Bundle?savedInstanceState)?{??
  • ????????//?這里的R.layout.right是界面的id??
  • ????????View?view?=?inflater.inflate(R.layout.right,?null);??
  • ????????Button?button?=?(Button)?view.findViewById(R.id.button11);??
  • ????????button.setOnClickListener(new?OnClickListener()?{??
  • ??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????Toast.makeText(getActivity(),?"hello?world!",?Toast.LENGTH_LONG)??
  • ????????????????????????.show();??
  • ????????????}??
  • ????????});??
  • ????????return?view;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onPause()?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onPause();??
  • ????}??
  • }??

  • MainActivity.java:

    [java]?view plaincopy
  • package?com.lc.tablet_fragment_addview;??
  • ??
  • import?android.app.Activity;??
  • import?android.app.FragmentManager;??
  • import?android.app.FragmentTransaction;??
  • import?android.os.Bundle;??
  • import?android.view.Menu;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.widget.Button;??
  • ??
  • public?class?MainActivity?extends?Activity?{??
  • ??
  • ????private?Button?button;??
  • ????private?FragmentManager?fragmentManager;?//?管理??
  • ????private?FragmentTransaction?fragmentTransaction;?//?事務??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.activity_main);??
  • ??
  • ????????button?=?(Button)?this.findViewById(R.id.button1);??
  • ????????fragmentManager?=?getFragmentManager();??
  • ??
  • ????????button.setOnClickListener(new?OnClickListener()?{??
  • ??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????fragmentTransaction?=?fragmentManager.beginTransaction();??
  • ????????????????MyFragment?myFragment?=?new?MyFragment();??
  • ????????????????//?第一個參數是要放到哪個地方的id,第二個為要放入的fragment??
  • ????????????????fragmentTransaction.add(R.id.linerlayout2,?myFragment);??
  • ????????????????fragmentTransaction.commit();??
  • ????????????}??
  • ????????});??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  • ????????//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.??
  • ????????getMenuInflater().inflate(R.menu.main,?menu);??
  • ????????return?true;??
  • ????}??
  • ??
  • }??
  • 演示效果:當點擊灰色界面的按鈕時顯示右側的布局:



    二、第二種方法

    項目結構和上圖中的差不多:只是在布局文件中,直接使用fragment控件: [html]?view plaincopy
  • <RelativeLayout?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"?>??
  • ??
  • ????<fragment??
  • ????????android:id="@+id/fragment1"??
  • ????????android:name="com.example.tablet_fragment_fragementmanager.MyFragment"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:layout_alignParentTop="true"??
  • ????????android:layout_centerHorizontal="true"??
  • ????????android:layout_marginTop="37dp"?/>??
  • ??
  • </RelativeLayout>??

  • 在myfragment.java文件中,只需找到fragment所容納的布局文件即可,不進行業務上的操作: [java]?view plaincopy
  • package?com.example.tablet_fragment_fragementmanager;??
  • ??
  • import?android.app.Fragment;??
  • import?android.os.Bundle;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.view.ViewGroup;??
  • ??
  • public?class?MyFragment?extends?Fragment?{??
  • ??
  • ????public?MyFragment()?{??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??
  • ????????????Bundle?savedInstanceState)?{??
  • ????????/*?
  • ?????????*?這里只需找到布局文件即可?
  • ?????????*/??
  • ????????View?view?=?inflater.inflate(R.layout.text,?null);??
  • ????????return?view;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onResume()?{??
  • ????????super.onResume();??
  • ????}??
  • ??
  • }??


  • MainActivity.java文件:進行fragment的業務處理 [java]?view plaincopy
  • package?com.example.tablet_fragment_fragementmanager;??
  • ??
  • import?android.app.Activity;??
  • import?android.app.FragmentManager;??
  • import?android.os.Bundle;??
  • import?android.view.Menu;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.widget.Button;??
  • import?android.widget.Toast;??
  • ??
  • /*?
  • ?*?再布局文件中拖入一個fragment、則使用下邊的方法來找到特定的fragment?
  • ?*?不需要使用beginTransaction方法?
  • ?*/??
  • public?class?MainActivity?extends?Activity?{??
  • ??
  • ????private?MyFragment?fragment;??
  • ????private?FragmentManager?fragmentManager;??
  • ????private?Button?button;??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.activity_main);??
  • ??
  • ????????fragmentManager?=?getFragmentManager();??
  • ????????//?使用fragmentManager找到fragment、使用ID作為唯一的標識符??
  • ????????fragment?=?(MyFragment)?fragmentManager??
  • ????????????????.findFragmentById(R.id.fragment1);??
  • ??
  • ????????//?或者使用下邊的方法找到fragment??
  • ??
  • ????????//?fragment?=(MyFragment)fragmentManager.findFragmentByTag("fragment1");??
  • ??
  • ????????//?找到fragment布局中的按鈕button1??
  • ????????button?=?(Button)?fragment.getView().findViewById(R.id.button1);??
  • ????????button.setOnClickListener(new?OnClickListener()?{??
  • ??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????Toast.makeText(MainActivity.this,?"hello?world!",??
  • ????????????????????????Toast.LENGTH_SHORT).show();??
  • ????????????}??
  • ????????});??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  • ????????getMenuInflater().inflate(R.menu.main,?menu);??
  • ????????return?true;??
  • ????}??
  • ??
  • }??
  • 總結

    以上是生活随笔為你收集整理的Android学习笔记之Fragment的两种使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 国产精品高清无码在线观看 | 高h校园不许穿内裤h调教 | 亚洲二区精品 | 日韩成人福利视频 | 色婷婷视频在线 | 久草免费资源 | 网站av | 特大黑人巨人吊xxxx | 在线亚洲区 | 国产第6页 | av在线短片 | 别揉我奶头啊嗯一区二区 | 久久精品国产亚洲av麻豆 | 羞羞色院91蜜桃 | 精品三级在线观看 | 狠狠爱成人| 日本无翼乌邪恶大全彩h | 国产福利视频一区 | 色哟哟一区二区三区 | 羽月希奶水一区二区三区 | 先锋资源av网 | 亚洲黄色片网站 | 高h1v| 雷电将军和丘丘人繁衍后代视频 | 午夜试看120秒 | aa一级视频 | 成人网站在线进入爽爽爽 | 爱av在线 | 91在线免费视频观看 | 国产欧美一区二区视频 | 国产精品久久久久毛片 | 国产又大又黄的视频 | 91黄色国产 | 操操操插插插 | 嫩草在线观看 | 91久久久久国产一区二区 | 情欲超| 美女黄色免费网站 | 丰满人妻一区二区三区免费 | 久久成人视屏 | 日本毛片在线 | 男人插女人免费视频 | 国产精品久久久国产盗摄 | 日韩熟妇一区二区三区 | 黄色国产在线 | 欧美人妖乱大交 | 91麻豆成人精品国产免费网站 | 性猛交╳xxx乱大交 偷偷操不一样的久久 | 四虎国产精品永久免费观看视频 | 91麻豆产精品久久久久久 | 丝袜美女啪啪 | 日韩天堂一区 | 色婷婷久久久亚洲一区二区三区 | 亚洲黄色一区 | 成人黄色国产 | 欧美一区二不卡视频 | 好吊色网站 | 欧美成人午夜电影 | 免费成人av在线 | 裸体喂奶一级裸片 | 亚洲高清在线免费观看 | 日韩大片一区 | 亚洲av无码一区二区三区网站 | 日韩一级片一区二区 | 蜜桃av鲁一鲁一鲁一鲁俄罗斯的 | 国产区亚洲区 | 国产精品久久久久久久久绿色 | 五月婷婷久| 精品1区2区| 在线视频欧美日韩 | 国产精品久久久久久网站 | 国语对白对话在线观看 | 日日插日日操 | 日韩大胆视频 | 人人看人人舔 | 九九热在线播放 | 丰满大乳国产精品 | 男人深夜影院 | 欧美在线三区 | 天天射天天干天天色 | 国产精品无码人妻一区二区在线 | 久久色图| 老司机福利院 | 浪漫樱花动漫在线观看免费 | 中文字幕在线免费看 | 亚洲欧美韩日 | 97超碰人人模人人人爽人人爱 | 美女视频网址 | 日韩av无码一区二区三区 | 久久免费在线观看视频 | 久草视频国产 | 亚洲精品视频免费观看 | 免费成人在线观看动漫 | 国产盗摄一区二区三区在线 | 国产探花一区 | 久久久久久久久久一区二区三区 | 欧美一区二区在线视频 | 在线精品视频一区 | 18禁裸乳无遮挡啪啪无码免费 |