android怎么布局这个布局是怎么弄的啊???

本帖子已过去太久远了,不再提供回复功能。【1-8】Android布局技巧与优化 - 简书

写了14962字,被13人关注,获得了12个喜欢
【1-8】Android布局技巧与优化
一、思维导图
二、重述知识
这节课主要讲了Android的布局是怎样绘制的,它是按层级一层层地绘制的,所以如果层次太多就会产生性能问题。这就需要我们去优化布局,有以下的这些手段:
多用相对布局,合理使用线性布局;
用&include/&标签去引用相同的部分
用&merge/&减少无用的层次
布局结构要清晰,删除无用的布局,选择合适的布局。
还可以利用“Android Lint”、“Hierarchy Viewer”这样的工具协助优化。另外,不要嵌套多个使用layout_weight属性的LinearLayout。
三、具体应用场景
略。(这些优化技巧,很难从应用长什么样子看出来,用起来才知道。)
四、扩展理解(优劣?有无替代方案?除了这样用,还能怎么用?)
这里推荐徐宜生的《Android群英传》P228~P234,有这方面内容的明细,其实根本不用写,看完、练完书上的也就学到了。
Android布局是怎样绘制出来的(是什么)?【简述】
在Android中,系统对View进行测量、布局和绘制时,都是通过对View数的遍历来进行操作的。
----《Android群英传》
简单来说,就是一层层深入,画完“子”那一层,再回去画“父”那层。
更深入的内容,请阅读《》这篇文章,我初学就先不看了。
为什么我这个应用这么卡(为什么)?
首先,回答这个问题,要先知道怎样才会觉得应用不卡。玩游戏的都知道,最佳fps是60fps,我们人眼能感觉到的流畅是40帧/秒到60帧/秒。在Android中, 1秒/60帧 约等于16ms。这个数据表示,我们在手机屏幕看到的所有UI界面的东西,都要保证在16ms内完成绘制,画不完的要等到下一次屏幕渲染,这样就会在16ms*2的时间内,显示同一帧画面,那我们人眼看出来的效果,就是“卡”了。
那我怎么优化才能不卡啊(怎样做)?
目前对我来说,就是上面说到那几样:优化布局层级、&include/&、&merge/&、&ViewStub/& ……
五、核心代码或操作
1.课程视频中的减少&LinearLayout/&的操作首先从第3分钟开始,老师以拖控件的方式,拖了一个布局出来,从“3:28~8:45”这段时间,过程如下所示:
前8分钟操作步骤 1
中间那个横向的&LinearLayout/&高度改为100dp,在中间那个&LinearLayout/&再加个&LinearLayout/&,再改一下大小、颜色之类的属性,就弄成下面最后的那幅图那样。
前8分钟操作步骤 2
代码大概是下面这样,这个是我自己拖的,不是视频里面的:
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
android:orientation="vertical"&
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
&LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="#567890"
android:gravity="right"
android:orientation="horizontal"&
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" /&
&LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="horizontal"&&/LinearLayout&
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" /&
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" /&
&/LinearLayout&
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" /&
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" /&
&/LinearLayout&
然后就可以开始减少层次了,过程如下:
[Gif备用链接]:()
2.课程视频中的&include/&的操作例如我有一个布局,要用到其它很多的Activity,没理由每个Activity再重新写一遍这个布局的代码,这时候就可以用&include/&标签,把这个需要重用的代码放到那些Activity需要的地方。
新建一个这样的 layout_title.xml 布局
在另一个Activity把它include进来
产生这样的效果
就像上面这样,在activity_main.xml中把layout_title.xml的布局&include/&进来。
(课程视频里面其它的优化技巧说得都不太好,看完别的资料,还需要写另一篇博文说一下。)
六、相关面试题
七、脑内记忆
主要记住下面这件代码:
&include layout="@layout/layout_title"/&
layout → 布局一般这句都是有两个"布局",那个@想成象棋棋子,两个"布局"include在一起,就是一幅象棋咯。(脑洞略大)
八、参考资料
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:一、在xml文件里可以直接设置:
android:background="#ffffff"
其他颜色可以看这里
二、在java文件里设置:
LinearLayout myLayout = () findViewById(R.id.linearLayout1);
myLayout.setBackgroundColor(Color.WHITE);
三、在AndroidManifest.xml里利用android:theme来设置,这个命令还是很强大的,如下所示:
theme的设置 可以设置为系统自带的格式,也可以自定义格式。
A: 系统自带格式
&&&@android:style/Theme.Black&&//背景黑色-有标题-非全屏
&&&@android:style/Theme.Black.NoTitleBar //背景黑色-无标题-非全屏
&&&@android:style/Theme.Black.NoTitleBar.Fullscreen //背景黑色-无标题-全屏显示
&&&@android:style/Theme.Dialog //对话框显示
&&&@android:style/Theme.InputMethod
&&&@android:style/Theme.Light&&&&//背景白色-有标题-非全屏
&&&@android:style/Theme.Light.NoTitleBar //背景白色-无标题-非全屏
&&&@android:style/Theme.Light.NoTitleBar.Fullscreen //背景白色-无标题-全屏显示
&&&@android:style/Theme.Light.Panel
&&&@android:style/Theme.Light.WallpaperSettings //背景透明
&&&@android:style/Theme.NoDisplay
&&&@android:style/Theme.Translucent.NoTitleBar.Fullscreen //半透明、无标题栏、全屏
&&&@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen
可以在单个Activity里设置,也可以在applicaiton里全局设置。比如:
&activity android:screenOrientation="portrait" android:name=".ui.RegisterActivity" android:theme="@android:style/Theme.NoTitleBar"&&/activity&
B:也可以自定义
&&&&&在activity里加入 android:theme="@style/MyTitleBar" 再在 style.xml里加入
&&&&style name="MyTitleBar" parent="android:Theme"&
&&&&&&&&&item name="android:windowTitleSize"&50dip&/item&
&&&&&&&&&&item name="android:windowTitleBackgroundStyle"&@style/MyTitleBackground&/item&
&&&&&&&&&item name="android:windowTitleStyle"&@style/WindowTitle&/item&
&&&/style&
&&!-- 自定义标题栏背景图 --&
&&&style name="MyTitleBackground" parent="android:TextAppearance.WindowTitle"&
&&&&item name="android:background"&@drawable/bg_topbar&/item&
&&&/style&
&&&style name="WindowTitle" parent="android:TextAppearance.WindowTitle"&
&&&&item name="android:singleLine"&true&/item&
&&&/style&
这里的parent是继承于android:Theme,所以在下面的样式里,只能是window开头的样式才起作用,所有样式请参考\sdk\docs\reference\android\R.attr.html,
也可以设置windowTitleBackgroundStyle 为@style/MyTitleBackground,这样就可以在MyTitleBackground里,设置背景图。
阅读(...) 评论()

参考资料

 

随机推荐