[关闭]
@act262 2017-03-25T17:26:39.000000Z 字数 3788 阅读 1723

Android开发中的一些小技巧

Android


一些小技巧可以优化布局,减少布局的嵌套,减少过度绘制,优化各项性能.


常常见到一个图标和文字并列的场景,比如水平排列的,左边一个图标右边有个文字,一般的实现用LinearLayout来水平排列ImageView和TextView。

  1. <LinearLayout
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:oritation="horizontal" >
  5. <ImageView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:src="@drawable/ic_tag" />
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="text" />
  13. </LinearLayout>

TextView有drawableLeft、drawableTop、drawableRight、drawableBottom属性可以设置上下左右的Drawable对象来替代上述的实现。
在布局设置,设置了才显示哪个方向的Drawable

  1. android:drawableLeft="@drawable/ic_tag"

代码中设置setCompoundDrawables,用null或0的参数表示不显示对应的位置(特别需要注意:设置的Drawable是必须是已调用过setBounds方法的,不然看不出效果),或者调用setCompoundDrawablesWithIntrinsicBounds内部会去处理drawable的bounds.

  1. setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)

Drawable与文字之间的距离可以通过drawablePadding来设置

  1. android:drawablePadding="10dp"
  1. setCompoundDrawablePadding(int pad)

优点:减少一层布局嵌套;
缺点:特殊的需求这个不好实现,可以考虑使用LayerDrawable实现一些特殊的需求 layer-list


正确使用ListView的分割线

ListView提供的分割线方法不能直接控制分割线的样式,但是可以控制分割线Drawable的样式,详细查看这篇文章http://blog.csdn.net/megatronkings/article/details/52156312
不推荐在布局里面直接插入一个View做为分割线,然后还要在代码中控制显示隐藏。可以使用InsetDrawable来修饰分割线的留白等来达到设计效果。

实现线性布局里面的分割线,一般做法是添加View作为分割线,实际上可以使用LinearLayout的

setDividerDrawable(Drawable divider)setShowDividers(int showDividers)方法来设置分割线,或者在xml布局中指定android:layout_divider.设置对应的参数即可显示分割线.

SHOW_DIVIDER_NONE,      // 默认的,不显示分割线
SHOW_DIVIDER_BEGINNING, // LinearLayout的开头显示分割线
SHOW_DIVIDER_MIDDLE,    // LinearLayout中每个子View之间显示分割线
SHOW_DIVIDER_END        // LinearLayout的尾部显示分割线

优点:减少分割线View的数量,直接绘制分割线效率更高.
缺点:不规则的分割线不好实现,可考虑搭配InsetDrawable实现

还有一些特殊的分割线,搭配各种Drawable可以自定义出来.
example:白色背景,顶部一条红色的分割线

  1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:drawable="@android:color/white" />
  3. <item android:gravity="top">
  4. <shape android:shape="rectangle">
  5. <solid android:color="#f00" />
  6. <size android:height="2dp" />
  7. </shape>
  8. </item>
  9. </layer-list>

有个需求是在一个ImageView上面覆盖一层半透明遮罩效果,一般做法是父布局用FrameLayout或者RelativeLayout,放上ImageView,然后再在上面覆盖一个背景半透明效果的View来实现。

  1. <FrameLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <ImageView
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:src="@mipmap/xxxx" />
  8. <View
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#7fff0000" />
  12. </FrameLayout>
  1. <FrameLayout
  2. android:layout_width="match_parent"
  3. android:foreground="#7fff0000"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:src="@mipmap/xxxx" />
  9. </FrameLayout>

布局中设置,可以使用Drawable或者Color

  1. android:foreground="@drawable/xx"

或者在代码中设置

  1. setForeground(Drawable foreground)

扩展:可以将图标作为FrameLayout的foreground来使用,可以减少一个View,需要注意对齐位置和拉伸问题

  1. <FrameLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="200dp"
  4. android:foreground="@mipmap/ic_player"
  5. android:foregroundGravity="center">
  6. <ImageView
  7. android:id="@+id/iv_thumb"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:scaleType="fitXY"
  11. android:background="@mipmap/default_image"/>
  12. <!--<ImageView-->
  13. <!--android:layout_width="wrap_content"-->
  14. <!--android:layout_height="wrap_content"-->
  15. <!--android:layout_gravity="center"-->
  16. <!--android:src="@mipmap/ic_player"/>-->
  17. </FrameLayout>


在全局中设置Window样式的背景色(图),而不是在每个根布局中单独设置背景.

给启动的目标Activity设置Window样式背景色(图),加速App加载视觉效果,而不是一片空白(或者黑漆漆的)后才看到页面效果.

可以设置

getWindow().setBackgroundDrawable(xx);

或者设置透明背景,启动时看上去没启动感觉

getWindow().setBackgroundDrawableResource(android.R.color.transparent);


使用Compat类

ViewCompat,DrawableCompat

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注