读《50 Android Hacks》笔记整理Hack 1~Hack 8

本文整理了《50 Android Hacks》的部分笔记,涵盖活用布局、性能优化的技巧,如使用weight属性实现视图居中、通过ViewStub延迟加载、创建自定义ViewGroup以提高效率。此外,还介绍了如何利用TextSwitcher和ImageSwitcher实现平滑过渡,以及为ViewGroup添加动画效果。
摘要由CSDN通过智能技术生成

[TOC]
最近看了《打造高质量Android应用》这本书,感觉是一本值得看看的书,不过里面也用挺多感觉冗余的地方。所以进行整理,等有时间我再想看的时候就不用在浪费时间了。

第一章 活用布局

Hack 1 使用weight属性实现视图的居中显示

需求与介绍:
因为不同Android设备的尺寸往往是不同的,所以我们需要进行不同尺寸屏幕的适配。硬编码是推荐的,因此需要使用其他方法来组织视图。
这个方法就是合用layout_weight和weightSum这两个属性来填充布局内部的任意剩余空间。
android:weightSum在开发文档中的解释如下:
“定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为综合的最大值。“
我们在使用layout_weight属性的时候一般会把layout_width或layout_height的值设置为0。因为,LinearLayout中的layout_weight属性,首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配。
计算方式=(总距离-控件声明的尺寸)值按weight分配。
而如果父控件设置了android:weightSum属性,layout_weight所属控件的width就由weightSum来决定。
示例:
以宽为200dp,android:weightSum属性值为1的LinerarLayout为例分析。计算这个控件的宽度公式如下:
Button’s width + Button’s weight * 200 / sum(weight)
因为指定了宽度为0dp,weight为0.5,sum(weight)等于1,所以结果如下:
0 +0.5 * 200 / 1 =100
外链地址


Hack 2 使用延迟加载以及避免代码重复

2.1 使用标签避免代码重复

需求与使用:
如我们需要为应用程序中每一个视图都添加一个页脚,它们都是相同的。这就可以通过标签把其他XML文件中定义的布局插入当前布局文件中。
使用的两种方法:
1.

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:text="hello" />

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

</RelativeLayout>

//footer_with_layout_properties.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"
    android:gravity="center_horizontal"
    android:text="world" />

缺点:
android:layout_alignParentBottom=”true”属性是RelativeLayout的如果在LinearLayout中就不起作用了。

2.直接在标签里使用android:layout_*属性

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:text="hello" />

    <include layout="@layout/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"/>

</RelativeLayout>

//footer.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:gravity="center"
    android:text="world" />

需要注意:
如果想在标签中覆盖被包含布局所指定的任何android:layout_*属性,必须标签中同时指定android:layout_width和android:layout_height这两个属性。

在方法二中我们把layout_width和ayout_height这两个属性都指定为0,目的是这样可以由使用footer.xml文件的人在标签中指定layout_width和ayout_height属性。如果不指定默认是0,就看不到页脚。

2.2 通过ViewStub实现View的延迟加载

需求与使用:
我们如果想要一个视图只在需要的时候显示就可以使用ViewStub这个类
在开发文档中的介绍:
“ViewStub是一种不可视并且大小为0点视图,可以延迟到运行时填充(inflate)布局资源。当ViewSt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值