写出高效布局的一些技巧

【威哥说】当你们都在谈论Android性能的时候,老是会习惯性的说怎么才能写出简洁高效的代码呢。每每老是忽略layout布局文件,布局缓慢的渲染速度对一个app的性能影响也是很大。充满没必要要的views和可读性差的layout文件会让你的app运行缓慢。一块儿来看下写出高效搞笑布局有哪些技巧吧!android

【正文】 1.用TextView自己的属性同时显示图片和文字。编程

一般须要在文本旁边添加一张图片,假设须要添加图片在文字的上边,如图:

输入图片说明

想必很多童鞋看到以后,首先想到的就是用一个LinerLayout或RelativeLayout来包含一个TextView和ImageView,最后须要用3个UI元素和大量的代码。可是有一个更好更清晰的解决方案,那就是TextView的compound drawable。你只须要一个属性就能够搞定。

<TextView
    android:layout_width="“wrap_content”"
    android:layout_height="“wrap_content”"
    android:drawablePadding="“5dp”"
    android:drawableTop="“@drawable/cat”"
    android:gravity="“center_horizontal”"
    android:text="“@string/cat”" >
</TextView>

用到的主要属性:

drawableTop : 指定drawable放在文本的顶部; DrawablePadding : 指定文本和drawable直接padding;app

2.用LinearLayout自带的分割线

分割线在app中常常会用到,LinearLayout有一个属性能够帮你添加分割线。以下图:LinearLayout包含2个TextView和基于它们中间的分割线。

输入图片说明 (1) Create divider shape(建立shape) 下面是一个简单的shape divider.xml用来看成分割线ide

<?xml version="1.0" encoding="utf-8"?>工具

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="3dp" /> <solid android:color="#000" /> </shape>布局

(2) Add shape to LinearLayout性能

<LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:divider="@drawable/divider" android:dividerPadding="5dp" android:orientation="horizontal" android:showDividers="middle" >3d

<TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="美好"
        android:textSize="16sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="生活"
        android:textSize="16sp" />

</LinearLayout>code

上面用到了3个xml属性:

Divider:用来定义一个drawable或color做为分割线。

showDividers:指定分割线在哪里显示,它们能够显示在开始位置,中间,末尾或者选择不显示。

Divider_Padding:给divider添加padding。

3.使用<include/>和<merge/>标签

重用布局是一个保持app一致的好方法,这样之后有改变的话只要改一个文件就能够了,Android提供了include标签帮你重用布局。

例如你如今决定建立一个有一个图片居中的Toolbar工具栏,而后你想要添加到每一个页面中,下面是Toolbar效果:

下面是toolbar.xml代码:

<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="50dp" android:background="#e5e5e5" android:orientation="horizontal" tools:context=".MainActivity" >xml

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:text="back"
    android:textSize="16sp" />

<ImageView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="4"
    android:gravity="center"
    android:src="@drawable/b" />

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:text="next"
    android:textSize="16sp" />

</LinearLayout>

你能够复制粘贴这些代码到每一个Activity,可是不要这么作,在编程中有一个重要的规则:当你复制粘贴,那你就是在作错误的事。在这种状况下你能够用include标签在多个界面重用这个布局。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

<include layout="@layout/toolbar"/>

</RelativeLayout>

用include标签你能够只用一行代码在app的每一个页面添加你的toolbar,任何改变都会自动更新到全部页面。

同时使用ImageView和background属性实现点击效果

你应该同时使用它们,在不少状况下你会想要给ImageView添加点击效果,而后我看到不少人用LinearLayout来包裹一个ImageView来实现。添加另外一个view是不必的。下面的代码可让你作的更好:

<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:background="?android:attr/selectableItemBackground" android:src="@drawable/a"/>

显示图片用”src”属性,drawable selector 图片点击效果用”background”属性实现,上面用的是android默认提供的selector,固然你也能够换成你本身实现的。

【总结】 但愿这些技巧能够帮你写出更好更简单的布局layout,可是不要把这些技巧当成是规则。总有一些比较复杂的布局,是无法用这些布局,那时候就须要经过增长不加的复杂性来解决问题。在这种状况下在添加更多的view以前,能够考虑自定义view试着找到更简单的解决方法。要牢记在视图层次添加一个view的代价是不可小嘘滴,它会影响app的加载速度。 输入图片说明

相关文章
相关标签/搜索