Android.Hacks读书笔记01


  #1#权重布局之解析:

    LinearLayout ’s android:weightSum      LinearLayout ’s child android: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.

    简单小Demo:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click me"/>
</LinearLayout>

至于为什么要把Button的宽度设置为0dp,是因为,在绘制界面的时候,会根据权重算出宽度然后再加上Button的宽度,求出的和才是Button的实际宽度,计算公式如下:

Button's width + Button's weight * 200 / sum(weight)

Because the Button ’s width is 0dp , the Button ’s weight is 0.5 . With the sum(weight)
set to 1 , the result would be the following:(假设屏幕宽度为200)
0 + 0.5 * 200 / 1 = 100

参考链接:http://developer.android.com/reference/android/view/View.html

  #2# 避免重复写一些共性布局,用<include>实现

  比较简单,小Demo如下:

  

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="@string/hello"/>
<include layout="@layout/footer_with_layout_properties"/>
</RelativeLayout/>

And the  footer_with_layout_properties would look like the following:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:text="@string/footer_text"/>

注意:如果父布局是相对布局,而<include>里面是线性布局,那么有可能属性会冲突,那就把<include>的xml文件的layout_.. 属性,写在<include width.. height...>

即可实现预期的效果。

#3#  ViewStub

  用ViewStub类和在XML文件里面指定的布局资源文件关联起来,让布局资源文件在需要使用的时候再加载上去。主要作用是性能优化,什么时候用什么时候加载,不用在开始启动的时候一次加载,既可以加快程序的启动速度,又可以节省内存资源

  小Demo:

  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="onShowMap"
android:text="@string/show_map" /> <ViewStub
android:id="@+id/map_stub"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inflatedId="@+id/map_view"
android:layout="@layout/map" /> <include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
layout="@layout/footer" /> </RelativeLayout>

布局比较简单,看不出优化效果,如果布局很复杂的话,那么优化性能就会很明显。

#4# 自定义viewgroup,以及PreferenceCategory的使用

首先要熟悉viewgroup在屏幕显示,绘制的过程,首先执行 let’s analyze the way to draw a ViewGroup . The first step is to measure its width and height, and we do this in the onMeasure() method. Inside that method, the ViewGroup will calculate its size by going through its children. We’ll make the final pass in the onLayout() method. Inside this second method, the View-Group will lay out its children using the information gathered in the onMeasure() pass.

通过2次view tree的遍历,把整个界面的绘制完成。

自定义控件参见源码即可。

--------------PreferenceCategory------Android配置界面的使用,比较简单实用。

TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合

android.content.res.TypedArray

包含函数 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtainAttributes(AttributeSet, int[])检索的数组值。

在执行完之后,一定要确保调用  recycle()函数 。用于检索从这个结构对应于给定的属性位置到obtainStyledAttributes中的值。

涉及的函数介绍:

obtainStyledAttributes(AttributeSet, int[], int, int)或者

obtainAttributes(AttributeSet, int[])

定义:

public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

public TypedArray obtainAttributes (AttributeSet set, int[] attrs)(说明此函数)

说明:返回一个由AttributeSet获得的一系列的基本的属性值,不需要用用一个主题或者/和样式资源执行样式。

参数:

set:现在检索的属性值;

attrs:制定的检索的属性值

public void recycle()

返回先前检索的数组,稍后再用。

/*******************************************************************************
* Copyright (c) 2012 Manning
* See the file license.txt for copying permission.
******************************************************************************/
package com.manning.androidhacks.hack004; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity; public class MainActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs); Preference sharePref = findPreference("pref_share");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this app!");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"Check this awesome app at: ...");
sharePref.setIntent(shareIntent); Preference ratePref = findPreference("pref_rate");
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
ratePref.setIntent(goToMarket); updateUserText();
} @Override
protected void onResume() {
super.onResume(); getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this); } @Override
protected void onPause() {
super.onPause(); getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
} @Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) { if (key.equals("pref_username")) {
updateUserText();
}
} private void updateUserText() {
EditTextPreference pref;
pref = (EditTextPreference) findPreference("pref_username");
String user = pref.getText(); if (user == null) {
user = "?";
} pref.setSummary(String.format("Username: %s", user));
}
}

  

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2012 Manning
See the file license.txt for copying permission.
--> <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="pref_first_preferencescreen_key"
android:title="Preferences"> <PreferenceCategory
android:title="User"> <EditTextPreference
android:key="pref_username"
android:summary="Username:"
android:title="Username"/> </PreferenceCategory> <PreferenceCategory
android:title="Application"> <Preference
android:key="pref_rate"
android:summary="Rate the app in the store!"
android:title="Rate the app"/> <Preference
android:key="pref_share"
android:summary="Share the app with your friends"
android:title="Share it"/> <com.manning.androidhacks.hack004.preference.EmailDialog
android:dialogIcon="@drawable/ic_launcher"
android:dialogTitle="Send Feedback"
android:dialogMessage="Do you want to send an email with feedback?"
android:key="pref_sendemail_key"
android:negativeButtonText="Cancel"
android:positiveButtonText="OK"
android:summary="Send your feedback by e-mail"
android:title="Send Feedback"/> <com.manning.androidhacks.hack004.preference.AboutDialog
android:dialogIcon="@drawable/ic_launcher"
android:dialogTitle="About"
android:key="pref_about_key"
android:negativeButtonText="@null"
android:title="About"/> </PreferenceCategory> </PreferenceScreen>

  详细参考代码见Android.Hack,系列源码==========================================================================

Android.Hacks.01_Centering views using weights的更多相关文章

  1. 50 Android Hacks阅读笔记

    Hack 1.善用weightSum和layout_weight. 问题提出:尝试做一个button的宽度是父View的一半的效果. 关键词:weightSum = 1 , layout_weight ...

  2. 面向忙碌开发者的 Android

    面向忙碌开发者的 Android passiontim 关注 2016.11.19 21:41* 字数 4013 阅读 2967评论 2喜欢 92 面向忙碌开发者的 Android 视频教程(Tuts ...

  3. Android Weekly Notes Issue #225

    Android Weekly Issue #225 October 2nd, 2016 Android Weekly Issue #225 本期内容包括: Android 7.0的Quick Sett ...

  4. 用Kotlin开发Android应用(IV):定制视图和Android扩展

    原文标题:Kotlin for Android (IV): Custom Views and Android Extensions 原文链接:http://antonioleiva.com/kotli ...

  5. Android App 开发技能图谱

    操作系统 Windows/MacOSX/Linux 编程语言 Java HTML/JS (Hybrid/Web App) C/C++ (NDK) SQL (DB) Kotlin 开发工具 IDE An ...

  6. 10本最新的Android开发电子书免费下载

    最新的Android开发电子书大集合,免费下载! 1.The Business of Android Apps Development, 2nd Edition http://ebook.goodfa ...

  7. Android 侧滑菜单的简单实现(SlidingMenu)二

    在上一篇博文中已经简单的实现了侧滑菜单,代码也很简单,就几行代码. 这篇文章依然讲侧滑菜单,与前一篇文章不同的是,这篇文章用不同的代码方式来实现侧滑菜单. 在前面的文章中已经用了在Activity中通 ...

  8. 【转】Android开发工具--android-studio-bundle-141.2288178

    原文网址:http://www.androiddevtools.cn/ AndroidDevTools简介 Android Dev Tools官网地址:www.androiddevtools.cn 收 ...

  9. [Android学习笔记]自定义控件的使用

    自定义控件时,最好抽象得彻底,并且编写需严谨,因为可能程序中多处都会引用到它,或者提供给团队中的其他人使用. 其一般步骤为: 1.创建控件的类文件,定义其功能逻辑.一般继承自现有控件或者View2.在 ...

随机推荐

  1. TCP的拥塞控制(转载)

    1.引言 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就是防止 ...

  2. <转>golang 并发性能数据

    1.管道chan吞吐极限10,000,000,单次Put,Get耗时大约100ns/op,无论是采用单Go程,还是多Go程并发(并发数:100, 10000, 100000),耗时均没有变化,Go内核 ...

  3. windows查询端口

    依次点击“开始→运行”,键入“cmd”并回车,打开命令提示符窗口.在命令提示符状态下键入“netstat -an”,按下回车键后就可以看到以数字形式显示的TCP和UDP连接的端口号及状态.

  4. js 常用正则表达式(不断维护中)

    身份证:pattern="/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/"

  5. Oracle表分区[转]

    废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table gra ...

  6. Oracle数据库中如何选择合适的索引类型 .

    索引就好象一本字典的目录.凭借字典的目录,我们可以非常迅速的找到我们所需要的条目.数据库也是如此.凭借Oracle数据库的索引,相关语句可以迅速的定位记录的位置,而不必去定位整个表. 虽然说,在表中是 ...

  7. Wordpress主题中常用代码总结

    1. 在 Wordpress主题中显示最热文章的 PHP代码 <?php $result = $wpdb->get_results("SELECT comment_count,I ...

  8. Sublime Text 3 个人配置文件

    { "dpi_scale": 1.0, "draw_white_space": "selection", "fallback_en ...

  9. Ice_cream's world I--hdu2120

    Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  10. PowerDesigner15在生成SQL时报错Generation aborted due to errors detected during the verification of the mod

    转载: http://blog.csdn.net/successful555/article/details/7582154 PowerDesigner中如何设置字符编码为GBK或者GB2312 ht ...