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:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#FFFFFF"
  6. android:gravity="center"
  7. android:orientation="horizontal"
  8. android:weightSum="1">
  9. <Button
  10. android:layout_width="0dp"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="0.5"
  13. android:text="Click me"/>
  14. </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如下:

  

  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:layout_centerInParent="true"
  9. android:gravity="center_horizontal"
  10. android:text="@string/hello"/>
  11. <include layout="@layout/footer_with_layout_properties"/>
  12. </RelativeLayout/>

And the  footer_with_layout_properties would look like the following:

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

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

即可实现预期的效果。

#3#  ViewStub

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

  小Demo:

  

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent" >
  4.  
  5. <Button
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:layout_gravity="center_vertical"
  9. android:onClick="onShowMap"
  10. android:text="@string/show_map" />
  11.  
  12. <ViewStub
  13. android:id="@+id/map_stub"
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent"
  16. android:inflatedId="@+id/map_view"
  17. android:layout="@layout/map" />
  18.  
  19. <include
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_alignParentBottom="true"
  23. android:layout_marginBottom="30dp"
  24. layout="@layout/footer" />
  25.  
  26. </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()

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

  1. /*******************************************************************************
  2. * Copyright (c) 2012 Manning
  3. * See the file license.txt for copying permission.
  4. ******************************************************************************/
  5. package com.manning.androidhacks.hack004;
  6.  
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.preference.EditTextPreference;
  13. import android.preference.Preference;
  14. import android.preference.PreferenceActivity;
  15.  
  16. public class MainActivity extends PreferenceActivity implements
  17. OnSharedPreferenceChangeListener {
  18.  
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. addPreferencesFromResource(R.xml.prefs);
  23.  
  24. Preference sharePref = findPreference("pref_share");
  25. Intent shareIntent = new Intent();
  26. shareIntent.setAction(Intent.ACTION_SEND);
  27. shareIntent.setType("text/plain");
  28. shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this app!");
  29. shareIntent.putExtra(Intent.EXTRA_TEXT,
  30. "Check this awesome app at: ...");
  31. sharePref.setIntent(shareIntent);
  32.  
  33. Preference ratePref = findPreference("pref_rate");
  34. Uri uri = Uri.parse("market://details?id=" + getPackageName());
  35. Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
  36. ratePref.setIntent(goToMarket);
  37.  
  38. updateUserText();
  39. }
  40.  
  41. @Override
  42. protected void onResume() {
  43. super.onResume();
  44.  
  45. getPreferenceScreen().getSharedPreferences()
  46. .registerOnSharedPreferenceChangeListener(this);
  47.  
  48. }
  49.  
  50. @Override
  51. protected void onPause() {
  52. super.onPause();
  53.  
  54. getPreferenceScreen().getSharedPreferences()
  55. .unregisterOnSharedPreferenceChangeListener(this);
  56. }
  57.  
  58. @Override
  59. public void onSharedPreferenceChanged(
  60. SharedPreferences sharedPreferences, String key) {
  61.  
  62. if (key.equals("pref_username")) {
  63. updateUserText();
  64. }
  65. }
  66.  
  67. private void updateUserText() {
  68. EditTextPreference pref;
  69. pref = (EditTextPreference) findPreference("pref_username");
  70. String user = pref.getText();
  71.  
  72. if (user == null) {
  73. user = "?";
  74. }
  75.  
  76. pref.setSummary(String.format("Username: %s", user));
  77. }
  78. }

  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright (c) 2012 Manning
  4. See the file license.txt for copying permission.
  5. -->
  6.  
  7. <PreferenceScreen
  8. xmlns:android="http://schemas.android.com/apk/res/android"
  9. android:key="pref_first_preferencescreen_key"
  10. android:title="Preferences">
  11.  
  12. <PreferenceCategory
  13. android:title="User">
  14.  
  15. <EditTextPreference
  16. android:key="pref_username"
  17. android:summary="Username:"
  18. android:title="Username"/>
  19.  
  20. </PreferenceCategory>
  21.  
  22. <PreferenceCategory
  23. android:title="Application">
  24.  
  25. <Preference
  26. android:key="pref_rate"
  27. android:summary="Rate the app in the store!"
  28. android:title="Rate the app"/>
  29.  
  30. <Preference
  31. android:key="pref_share"
  32. android:summary="Share the app with your friends"
  33. android:title="Share it"/>
  34.  
  35. <com.manning.androidhacks.hack004.preference.EmailDialog
  36. android:dialogIcon="@drawable/ic_launcher"
  37. android:dialogTitle="Send Feedback"
  38. android:dialogMessage="Do you want to send an email with feedback?"
  39. android:key="pref_sendemail_key"
  40. android:negativeButtonText="Cancel"
  41. android:positiveButtonText="OK"
  42. android:summary="Send your feedback by e-mail"
  43. android:title="Send Feedback"/>
  44.  
  45. <com.manning.androidhacks.hack004.preference.AboutDialog
  46. android:dialogIcon="@drawable/ic_launcher"
  47. android:dialogTitle="About"
  48. android:key="pref_about_key"
  49. android:negativeButtonText="@null"
  50. android:title="About"/>
  51.  
  52. </PreferenceCategory>
  53.  
  54. </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. Crystal Report分組中的序號重新遞增

    客戶要批次列印發票,也就是報表需要按照發票號碼(INV_NO)進行分組,每個發票里還有明細的item,之前因為直接抓RecordNumber,所以該欄位只能從1開始計數,遇到新的發票發號不會重新從1開 ...

  2. mysql三张表关联查询

    三张表,需要得到的数据是标红色部分的.sql如下: select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b. ...

  3. JSON数据的基础使用

    之前一直把JSON想做一种数据类型,通过这几天的使用,发现其实JSON只是一种数据的格式,而与int string double等等数据类型是有本质的区别. JSON(JavaScript Objec ...

  4. 变态最大值--nyoj题目811

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  5. UVA 712 S-Trees

    二叉树? 怒水~~ 注意一下查询与x值的对应关系就好~ #include <iostream> #include <cstring> #include <cstdio&g ...

  6. SemaphoreFullException when checking user role via ASP.NET membership

    将指定的计数添加到该信号量中会导致其超过最大计数 This issue was fixed by restarting ASP.NET Development Server on windows ta ...

  7. Oracle EBS-SQL (PO-17):检查供货比例不为100%.sql

    select           * from           apps.MRP_SOURCING_RULES msrwhere           organization_id=X.    a ...

  8. Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件

    Chapter 11. Frame, MainWindow, and Toplevel Widgets   框架,主窗体,顶级部件 框架和Toplevels 都是设计用于其他部件的容器. 它们的不同在 ...

  9. 在Eclipse中创建打开文件夹快捷键

    Run 展开如下菜单: Run ---- External Tools ---- External Tools Configurations 在 program 下面新建一个工具 在 Location ...

  10. Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...