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

网站开发 定制 合同 模板搜狐做app的网站

网站开发 定制 合同 模板,搜狐做app的网站,美团网站开发形式,晚上必看正能量网站短视频引言 1.重要性 在Android应用开发中#xff0c;布局是用户界面的基础。一个高效的布局不仅能提升用户体验#xff0c;还能显著改善应用的性能。随着应用功能的复杂性增加#xff0c;布局的优化变得尤为重要。优化布局能够减少渲染时间#xff0c;提高响应速度#xff0c…引言 1.重要性 在Android应用开发中布局是用户界面的基础。一个高效的布局不仅能提升用户体验还能显著改善应用的性能。随着应用功能的复杂性增加布局的优化变得尤为重要。优化布局能够减少渲染时间提高响应速度从而让用户获得更流畅的体验。 2.布局对性能的影响 布局直接影响应用的启动时间和运行时性能。嵌套过深的布局会导致测量和绘制的开销增加进而引发界面卡顿或不流畅的问题。此外复杂的布局会消耗更多的内存可能导致应用崩溃。通过合理的布局设计可以降低CPU和GPU的负担提升整体应用的运行效率。 布局性能的基本概念 布局性能是指在Android应用中布局系统处理视图View和视图组ViewGroup的效率。它主要涉及以下几个方面 测量性能 指计算视图大小所需的时间和资源。高效的测量可以减少布局过程中CPU的占用。 布局性能 这是指在视图层级中定位和排列视图所需的时间。层级过深或过于复杂的布局会导致性能下降。 渲染性能 涉及将布局绘制到屏幕上所需的时间和资源。有效的渲染能够确保界面快速响应用户操作。 内存使用 低内存使用意味着更少的GC垃圾回收从而减少卡顿和延迟。布局性能应关注内存的合理分配和使用。 优化布局文件 使用ConstraintLayout的优势 约束系统Constraint System ConstraintLayout 提供了一套完整的约束系统可以通过相对定位、边距、比例、链等特性来定义子视图之间的关系。这样一来不同视图之间可以在同一层级内相互依赖避免了层层嵌套的问题。 灵活的相对定位 使用传统布局如LinearLayout、RelativeLayout时通常需要多个嵌套来满足复杂的布局需求。例如一个水平和垂直居中的布局需要嵌套两个 LinearLayout 或者 RelativeLayout。而在 ConstraintLayout 中只需要设置 layout_constraint 属性即可将视图居中显示。 减少布局重绘和测量次数 每增加一个层级的嵌套都会增加布局的测量和绘制时间。而 ConstraintLayout 将大部分的布局逻辑集中在一个 ConstraintLayout 中进行处理减少了层级数量从而提升了性能。 替代多种传统布局 ConstraintLayout 可以同时替代 RelativeLayout、LinearLayout、FrameLayout 等多种布局。对于一些复杂的布局需求只需要一个 ConstraintLayout 就能完成无需再嵌套使用其他布局容器。 链式布局Chains ConstraintLayout 支持链式布局可以将多个视图通过链条的形式连接在一起根据指定的分布方式如均匀分布、权重分布等来排列视图。这种方式不仅减少了嵌套层级还能更加灵活地管理视图的排列。 Guideline 和 Barrier ConstraintLayout 还提供了 Guideline 和 Barrier 等辅助控件帮助开发者在同一层级内实现复杂的布局逻辑。例如通过 Guideline 可以在布局中定义固定的比例参考线方便其他视图的对齐和排列Barrier 则可以动态地将多个视图的边界作为参考点灵活适应视图的变化。 ConstraintLayout的使用可以参考以下博客【Android】ConstrainLayout约束布局基本操作_android studio的constrain layout可以删除吗-CSDN博客 include使用 include标签可以允许在一个布局当中引入另外一个布局那么比如说我们程序的所有界面都有一个公共的部分这个时候最好的做法就是将这个公共的部分提取到一个独立的布局文件当中然后在每个界面的布局文件当中来引用这个公共的布局。 下面举一个简单的例子 我们应该都知道目前几乎所有的软件都会有一个头布局头布局中可以包含界面的标题、返回按钮、以及其它一些操作功能。有些软件是使用ActionBar来实现的但是由于ActionBar的灵活性不太好因而也有很多软件会选择自己去编写实现。因为应用中几乎所有界面都要用头布局所以这种情况下使用include标签就非常合适了。 我们创建title_bar.xml ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parent Buttonandroid:idid/backandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_alignParentLefttrueandroid:layout_centerVerticaltrueandroid:textBack /TextViewandroid:idid/titleandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_centerInParenttrueandroid:textTitleandroid:textSize20sp /Buttonandroid:idid/doneandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_alignParentRighttrueandroid:layout_centerVerticaltrueandroid:textDone //RelativeLayouttitle_bar.xml中的布局非常简单外层是一个RelativeLayout里面只有两个Button和一个TextView左边的Button用于实现返回功能右边的Button用于实现完成功能中间的TextView则可以用于显示当前界面的标题。 在其他布局进行使用的时候直接用include就可以了 include layoutlayout/title_bar /这样我们在修改title_bar中内容的时候就不用一个页面一个页面去修改了直接修改文件里的就可以了。 但是我们会发现引入title_bar后我们原本布局中的内容全不见了原因是因为titlebar的最外层布局是一个宽高都是match_parent的RelativeLayout它会将整个布局都填充满因而我们原本的布局也就看不见了。我们可以将RelativeLayout的layout_height属性修改成wrap_content include layoutlayout/title_barandroid:layout_widthmatch_parentandroid:layout_heightwrap_content/除了layout_height之外我们还可以覆写titlebar中的任何一个layout属性如layout_gravity、layout_margin等而非layout属性则无法在include标签当中进行覆写。但是在覆写的时候必须要将layout_width和layout_height这两个属性也进行覆写否则覆写效果将不会生效。 merge使用 merge标签是作为include标签的一种辅助扩展来使用的它的主要作用是为了防止在引用布局文件时产生多余的布局嵌套。 在上面我们讲解include标签的用法时主要介绍了它优点但是它也存在着一个不好的地方就是可能会导致产生多余的布局嵌套。 新建ok_cancel_layout.xml ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationvertical Buttonandroid:idid/okandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:textOK /Buttonandroid:idid/cancelandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:layout_marginTop10dpandroid:textCancel //LinearLayout定义了两个按钮。 当activity_main中需要输入些东西引用上面两个按钮的时候可以使用include引用 include layoutlayout/ok_cancel_layout/?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:idid/mainandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityinclude layoutlayout/title_barandroid:layout_widthmatch_parentandroid:layout_heightwrap_content/EditTextandroid:idid/editandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginBottom10dpandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:layout_marginTop10dpandroid:hintEdit something here /include layoutlayout/ok_cancel_layout//LinearLayout运行效果如下 但是此时这个界面中已经存在着多余嵌套了 最外层首先是一个FrameLayout 在setContentView()方法中Android会自动在布局文件的最外层再嵌套一个FrameLayout。 具体原因可以参考博客Android LayoutInflater原理分析带你一步步深入了解View(一)_android layoutinflater原理分析,带你一步步深入了解view(一)-CSDN博客 然后FrameLayout中包含的是一个LinearLayout在最外层的LinearLayout当中包含了两个元素一个是EditText另一个又是一个LinearLayout然后在这个内部的LinearLayout当中才包含了确定和取消这两个按钮。 我们此时就可以用merge来优化布局。修改ok_cancel_layout.xml中的代码 ?xml version1.0 encodingutf-8? merge xmlns:androidhttp://schemas.android.com/apk/res/androidButtonandroid:idid/okandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:textOK /Buttonandroid:idid/cancelandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:layout_marginTop10dpandroid:textCancel //merge这里我们将ok_cancel_layout最外层的LinearLayout布局删除掉换用了merge标签这就表示当有任何一个地方去include这个布局时会将merge标签内包含的内容直接填充到include的位置不会添加任何额外的布局结构。 当前的View结构就变成了 现在EditText和两个按钮都直接包含在了LinearLayout下面去掉了多余的嵌套。 ViewStub使用 有的时候我们会遇到这样的场景就是某个布局当中的元素非常多但并不是所有元素都一起显示出来的而是普通情况下只显示部分常用的元素而那些不常用的元素只有在用户进行特定操作的情况下才会显示出来。 虽然设置其它元素可见不可见也可以实现该操作但是相应的元素还是会进行加载。那么我们如何才能让这些不常用的元素仅在需要时才去加载呢Android为此提供了一种非常轻量级的控件ViewStub。ViewStub虽说也是View的一种但是它没有大小没有绘制功能也不参与布局资源消耗非常低将它放置在布局当中基本可以认为是完全不会影响性能的。 我们新建profile_extra.xml ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical EditTextandroid:idid/edit_extra1android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:hintExtra field 1 /EditTextandroid:idid/edit_extra2android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:layout_marginTop10dpandroid:hintExtra field 2 /EditTextandroid:idid/edit_extra3android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:layout_marginTop10dpandroid:hintExtra field 3 //LinearLayout现在定义了三个EditText当作不常用控件。 修改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:idid/mainandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityinclude layoutlayout/title_barandroid:layout_widthmatch_parentandroid:layout_heightwrap_content/EditTextandroid:idid/editandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginBottom10dpandroid:layout_marginLeft20dpandroid:layout_marginRight20dpandroid:layout_marginTop10dpandroid:hintEdit something here /Buttonandroid:idid/moreandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_gravityrightandroid:layout_marginRight20dpandroid:layout_marginBottom10dpandroid:textMore /ViewStubandroid:idid/view_stubandroid:layoutlayout/profile_extraandroid:layout_widthmatch_parentandroid:layout_heightwrap_content/include layoutlayout/ok_cancel_layout//LinearLayout我们新增了一个More Button用来加载不常用元素后在Button的下面定义了一个ViewStub。通过layout属性将profile_extra布局传入进来。 下来在MainActivity里添加点击事件 public class MainActivity extends AppCompatActivity {private EditText editExtra1;private EditText editExtra2;private EditText editExtra3;private Button moreButton;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - {Insets systemBars insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});moreButton (Button) findViewById(R.id.more);moreButton.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {ViewStub viewStub (ViewStub) findViewById(R.id.view_stub);if (viewStub ! null) {View inflatedView viewStub.inflate();editExtra1 (EditText) inflatedView.findViewById(R.id.edit_extra1);editExtra2 (EditText) inflatedView.findViewById(R.id.edit_extra2);editExtra3 (EditText) inflatedView.findViewById(R.id.edit_extra3);}}});} }ViewStub 在布局加载时并不会立即把其内容添加到视图层次结构中它只占用极少的内存占位。当你调用 inflate() 方法时ViewStub 才会将其引用的布局资源加载到内存中并将其转换为一个普通的 View即 ViewGroup 或其他控件。 当点击More Button之后我们首先会调用findViewById()方法将ViewStub的实例获取到调用inflate()方法或者setVisibility(View.VISIBLE)都可以将隐藏的布局给加载出来而加载的这个布局就是刚才在XML当中配置的profile_extra布局。 效果如下 点击前 点击后 小结 本篇博客主要分享了对于布局的优化方面的知识并讲解了includemergeViewStub的使用方法希望对大家有所帮助。 参考Android最佳性能实践(四)——布局优化技巧_android 在代码里生成布局性能-CSDN博客 已经到底啦
http://www.hkea.cn/news/14273073/

相关文章:

  • 网站开发转码手机开发公司资质查询
  • c2c电子商务网站建设进销存软件
  • 网站建社石家庄公司网站怎么选
  • 网站开发环境介绍模板网站优化
  • 冀州网站建设价格百度百家号注册
  • 做网站公司怎么样wordpress 相册浏览器
  • 企业网站flash网站建设运营工作业绩
  • 辽宁鞍山网站建设公司wordpress前端用户
  • 蓝彩网络科技_齐齐哈尔微信营销_齐齐哈尔网站建设网站建设需要自备什么
  • 网站备案对网站负责人的要求网站建设制
  • 国外的设计网站安装文件出现乱码
  • 网站建设 引导物流行业网站建设市场分析
  • 35互联做网站多少钱厦门购买域名以后搭建网站
  • 向自己做网站上海网页制作多少钱
  • 中国建设工程招标网站顺企网属于什么网站
  • 网站做游戏活动策划方案WordPress破解分享
  • 仿wordpress站免费门户网站搭建
  • 域名备案查询网站备案陕西建设网官方网站
  • 网站开发盈亏平衡分析表安徽股票配资网站建设
  • 做外贸网站基本流程网站建设的功能有哪些内容
  • 网站推广一般办法太原深圳建设工程信息网站
  • 手机百度网站证书过期浙江省建设教育考试中心网站
  • 网站建设合同需要缴纳印花税网站建设济南
  • 建立网站需要多少人网站春节放假
  • linux 网站开发wordpress汉化教程视频
  • 网站建设总体规划包括哪些黑色大气网站源码
  • 江西建设厅网站查询施工员九游手游平台app
  • 网站建设实验周志与总结制作网站单页
  • 百度数据网站电子商务网站建设策划书 服装 有详细数据库设计
  • 因脉网站建设公司怎么呀韩国萨wordpress