Android开发中这些小技巧你都知道吗?(四)

本文介绍了Android开发中的一些实用技巧,包括Activity的isChangingConfigurations()方法判断配置变化、ViewTreeObserver监听视图状态变化、DatabaseUtils的数据库操作、布局权重属性weightSum的使用,以及clipChildren、fillViewport、tileMode、enterFadeDuration/exitFadeDuration和scaleType等属性的详细解释。同时提到了<merge>标签在优化UI结构中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处:http://blog.csdn.net/guxiao1201/article/details/40708775

Activity public boolean isChangingConfigurations ()
Added in  API level 11

Check to see whether this activity is in the process of being destroyed in order to be recreated with a new configuration. This is often used in onStop() to determine whether the state needs to be cleaned up or will be passed on to the next instance of the activity via onRetainNonConfigurationInstance().

Returns
  • If the activity is being torn down in order to be recreated with a new configuration, returns true; else returns false.
ViewTreeObserver

  • 可以注册监听正在屏幕上显示的视图树中任何视图状态的变化,我经常用来视图初始化完成后获取某个控件的尺寸。

A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.... A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to getViewTreeObserver() for more information.

DatabaseUtils
  • 一个包装了一系列数据库操作方法的工具类(不过我没用过,还是喜欢手动敲sql)

android:weightSum

  • 如果想实现一个Button,宽度占据屏幕宽度的50%怎么办?代码动态计算?尝试结合使用android:weightSum和android:layout_weight吧。参考博客:合用weightSum属性和layout_weight属性

Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

Must be a floating point value, such as "1.2".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol weightSum.

android:duplicateParentState/android:addStatedFromChildren

  • 使用这个属性来应对那些挑剔的UI再好不过了。之前还苦恼父控件和点击状态怎么和子控件同步,直到看到这个属性真实相见恨晚。但使用过程中需注意这两个属性只是传递点击状态而不会执行点击事件。参考博客:Android View与View之间的状态关联处理

When this attribute is set to true, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol duplicateParentState.

android:clipChildren

  • 设置这个属性后子控件就可以在父控件的范围之外进行绘制了,编写动画时再也不用一层多余的FrameLayout。

Defines whether a child is limited to draw inside of its bounds or not. This is useful with animations that scale the size of the children to more than 100% for instance. In such a case, this property should be set to false to allow the children to draw outside of their bounds. The default value of this property is true.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol clipChildren.

Related Methods
android:fillViewport

  • 当开发者需要设置一个内容不足以填充整个屏幕的ScrollView全屏时,设置fill_parent是不起作用的,那么使用这个属性吧。

Defines whether the scrollview should stretch its content to fill the viewport.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol fillViewport.

android:tileMode

  • 用来设置Bitmap显示方式,有平铺、重复等。例如设置重复显示
<xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
android:id="@+id/MainLayout"  
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
android:orientation="vertical"  
android:background="@drawable/backrepeat"  
>  
backrepeat.xml
<bitmap   
    xmlns:android="http://schemas.android.com/apk/res/android"   
    android:src="@drawable/repeatimg"   
    android:tileMode="repeat"   
    android:dither="true" />



Defines the tile mode. When the tile mode is enabled, the bitmap is repeated. Gravity is ignored when the tile mode is enabled. Default value is "disabled".

Must be one of the following constant values.

Constant Value Description
disabled -1 Do not tile the bitmap. This is the default value.
clamp 0 Replicates the edge color.
repeat 1 Repeats the bitmap in both direction.
mirror 2 Repeats the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam.

This corresponds to the global attribute resource symbol tileMode.

android:enterFadeDuration/android:exitFadeDuration
Added in  API level 11

  • 为selector设置渐变效果,示例:
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:enterFadeDuration="500"
    android:exitFadeDuration="500">

    <item android:state_focused="true"
        android:drawable="@color/press"/>
    <item
        android:state_pressed="true"
        android:drawable="@color/press"/>
    <item
        android:drawable="@color/normal"/>
</selector>


Amount of time (in milliseconds) to fade out an old state drawable.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

Constant Value: 16843533 (0x0101030d)
android:scaleType

Controls how the image should be resized or moved to match the size of this ImageView.

Must be one of the following constant values.

Constant Value Description
matrix 0  
fitXY 1  
fitStart 2  
fitCenter 3  
fitEnd 4  
center 5  
centerCrop 6  
centerInside 7  

This corresponds to the global attribute resource symbol scaleType.

<merge> 

参考资料:

Android Tips Round-Up, Part 4

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值