当前位置: 首页 > news >正文

盐城网站开发渠道合作重庆腊肠制作

盐城网站开发渠道合作,重庆腊肠制作,网络软文怎么写,网站关键词没有排名Android Fragment 基本概念和基本使用 一、基本概念 Fragment#xff0c;简称碎片#xff0c;是Android 3.0#xff08;API 11#xff09;提出的#xff0c;为了兼容低版本#xff0c;support-v4库中也开发了一套Fragment API#xff0c;最低兼容Android 1.6。 过去s…Android Fragment 基本概念和基本使用 一、基本概念 Fragment简称碎片是Android 3.0API 11提出的为了兼容低版本support-v4库中也开发了一套Fragment API最低兼容Android 1.6。 过去support-v4库是一个jar包24.2.0版本开始将support-v4库模块化为多个jar包包含support-fragment, support-ui, support-media-compat等这么做是为了减少APK包大小你需要用哪个模块就引入哪个模块。 Android 官方对Fragment的解释如下 A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. Fragment表示Activity中的行为或user interface的一部分。可以在单个Activity中组合多个Fragment来构建多窗格 UI 并在多个活动中复用Fragment。可以将Fragment视为Activity的模块化部分它具有自己的生命周期接收自己的输入事件并且可以在Activity运行时添加或删除 由此可知 Fragment是依赖于Activity的不能独立存在的。一个Activity里可以有多个Fragment。一个Fragment可以被多个Activity重用。Fragment有自己的生命周期并能接收输入事件。我们能在Activity运行时动态地添加或删除Fragment。 Fragment的优势有以下几点 模块化Modularity我们不必把所有代码全部写在Activity中而是把代码写在各自的Fragment中。可重用Reusability多个Activity可以重用一个Fragment。可适配Adaptability根据硬件的屏幕尺寸、屏幕方向能够方便地实现不同的布局这样用户体验更好。 Fragment核心的类有 FragmentFragment的基类任何创建的Fragment都需要继承该类。FragmentManager管理和维护Fragment。他是抽象类具体的实现类是FragmentManagerImpl。FragmentTransaction对Fragment的添加、删除等操作都需要通过事务方式进行。他是抽象类具体的实现类是BackStackRecord。 二、基本使用 在res/layout目录下新建fragment1.xml其中内容如下 ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:background#FF00FFTextViewandroid:idid/fragment1_textandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textthis is fragment1android:textColor#000000android:textSize25sp//LinearLayout在res/layout目录下新建fragment2.xml其中内容如下 ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:background#FFFF00TextViewandroid:idid/fragment2_textandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textthis is fragment2android:textColor#000000android:textSize25sp //LinearLayout在res/layout目录下activity_main.xml内容如下 ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityfragmentandroid:idid/fragment1android:namecom.example.fragmentdemo.FragmentOneandroid:layout_width0dipandroid:layout_heightmatch_parentandroid:layout_weight1 /fragmentandroid:idid/fragment2android:namecom.example.fragmentdemo.FragmentTwoandroid:layout_width0dipandroid:layout_heightmatch_parentandroid:layout_weight1 //LinearLayout新建的FragmentOne类继承Fragment在onCreateView里通过inflate(R.layout.fragment1, container, false)加载刚才写过的fragment1.xml里的布局 package com.example.fragmentdemo;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 FragmentOne extends Fragment {public static final String TAG Fragment1;Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {Log.d(TAG, onCreateView);return inflater.inflate(R.layout.fragment1, container, false);} }新建的FragmentTwo类继承Fragment在onCreateView里通过inflate(R.layout.fragment1, container, false)加载刚才写过的fragment2.xml里的布局 package com.example.fragmentdemo;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; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;public class FragmentTwo extends Fragment {public static final String TAG Fragment2;Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {return inflater.inflate(R.layout.fragment2, container, false);} }MainActivity内容如下 package com.example.fragmentdemo;import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment;import android.os.Bundle; import android.view.Display;public class MainActivity extends AppCompatActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);} }整体的目录结构如下 显示出来的效果如下所示 其中如果一个Activity里这种分割开来的片段较多即可采取多个fragment来组成。 三、Fragment之间如何通信 getActivity方法可以让Fragment获取到关联的Activity然后再调用Activity的findViewById方法就可以获取到和这个Activity关联的其它Fragment的视图了。 比如上文中的第二个Fragement需要获取第一个Fragment的文本内容的话就可以通过 在onActivityCreated方法中这样来获取 Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);TextView textView1 getActivity().findViewById(R.id.fragment1_text);TextView textView2 getActivity().findViewById(R.id.fragment2_text);// 设置第二个Fragement的文本为第一个Fragment的文本textView2.setText(textView1.getText());Log.d(TAG, onActivityCreated);}参考https://blog.csdn.net/guolin_blog/article/details/8881711
http://www.yutouwan.com/news/71279/

相关文章:

  • 东莞网站建设的价格天津高端网站定制
  • 口碑好网站建设费用wordpress付款后查看内容
  • 网站后期维修问题应用公园下载
  • 如何做团购网站网站怎么屏蔽ip
  • 桂林出网站网站建设与运营收入预算
  • 比较出名做耐克的网站怎么找wordpress博客
  • 网站怎么自己做中国网络安全厂商排名
  • 电商网站首页设计如何提升网站搜索排名
  • 网站改版页面不收录建设行政主管部门政务网站
  • 不收费的企业查询网站免费下载android
  • 长沙做网站找谁wordpress音乐美化
  • 大学生做网站步骤如何建立一个网站的快捷方式
  • 旅游网站图片网站有几个后台
  • html网站的直播怎么做的品牌策划 品牌年度服务
  • 网站关键字 优帮云广元市规划和建设局网站
  • 网站建设归工商局管还是工信局管大连网站制作案例
  • 深圳 网站托管免费企业网站模板 php
  • 网站开发公司杭州网站建设网站左侧 导航
  • 上海市建设安全协会网站孟 侠厦门建设局投诉电话
  • led高端网站建设潍坊专业汽车贴膜
  • 舟山网站建设制作thinkphp cms开源系统
  • 网站开发建设推荐用书app维护费用一般多少钱
  • 网站页面报价怎样做外贸网站推广
  • 建筑效果图网站推荐免费网站制作作业
  • 蓝色风格的网站株洲做网站多少钱
  • 小城镇建设投稿网站赣州人才网招聘网
  • 网站建设合同印花税大气自适应网站源码
  • 佛山市网站开发win7怎么做网站映射
  • 北京网站建设公司桂林小程序制作
  • 做网站要会写什么软件购物网站数据分析