50 Android hacks(hack 1)

接触到一本涉及android编程技巧的书,叫做《50 Android Hacks》,作者:Carlos Sessa。然后从网上找了一下这本书的译本,居然发现没有免费中文版的,所以和技术群里的小伙伴们商量翻译一下,以飨程序猿。后来发现初衷虽好,但是路漫漫,每天下班的一点时间充斥着乱七八糟的事,所以就先以博客的形式,以单个技巧为单位发到博客上,最后再整理成完整的pdf。希望大家用的上。
在翻译中,我尽量的保持了原来语句的语义,但是受“chinese english”的影响,也难免有偏差,希望大家批评指正。

先来张原书的封面,致敬作者。


Start:

第一章 按你的想法布置层

       在本章里,我们将谈到安卓布局的技巧。你将会学到如何从草稿或已有的布置创造出特定的样子。

技巧1 利用weights居中视图

       在我给程序员做的一次安卓报告中,当我正在讲如何用xml文件创建一个视图时,有人问:“如果我想让一个按钮居中并且占据父窗口宽度的一半,我该怎么写?”起初,我并不理解他所说的,但是当他在黑板上画出来后,我理解了。他的意思描述在图1.1和1.2中。


       看起来很简单,是吧?现在我们用几分钟来完成它。在该技巧里,我们将演示如何使用LinearLayout的android:weightSum属性和子控件的android:layout_weight属性解决这个问题。这看起来是个简单的问题,但是有时我与一些工程师交流的时候,经常问这个问题,因为他们中很多人不知道解决的最佳办法。

1.1 Weightsum和layout_weight同时使用

        安卓设备有不同的尺寸,作为开发者,我们常常需要为不同尺寸的屏幕创建XML。直接指定尺寸不是一个好办法,所以我们需要一些东西来规划视图。

我们使用layout_weight和weightSum属性来填充布局的任意空间。Android:weightSum文档(1.3节)描述了一个我们能达到目标的方案:

定义最大的weight和。如果未指定,该和是所有子控件的layout_weight和。这样我们能做出这样一个实例:界面中只有一个子控件,设置他的layout_weight为0.5,设置weight和为1.0,这样子控件就占所有可用空间的50%。

        假如我们需要在盒子中放置填充物。可用空间的比例就是weightSum,而layout_weight就是盒子内部物品所占空间的百分比。例如,假设盒子的weightSum是1,盒子中有两个物品,A和B。A的layout_weight为0.25,B的layout_weight为0.75。所以物品A占据了25%的盒子空间,而B则是剩余的75%。

我们在本章开始所说的问题是类似的。我们设定父控件的weightSum值,然后给按钮Android:Layout_weight设该值的一半,最后的xml为:

<?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="@string/activity_main_click_me" />

</LinearLayout>

LinearLayout读android:weightSum属性的值,这样就知道了所有子控件的weight和应该是1。它唯一一个控件是按钮,因为按钮的宽度android:layout_width设置为0dp,Linearlayout必须利用android:weightSum值和可用的空间决定按钮的宽度。因为按钮的android:layout_weight设置为0.5,它将占据50%的可用空间。

       一个例子:LinearLayout的android:weightSum设置为1,实际空间宽度为200dp,那么按钮的宽度这样计算:

按钮宽度+按钮weight*200/总和(weihgt)

     因为按钮的宽度是0dp,按钮的weight是0.5,总和(weihgt)是1,最终的结果

     0+0.5*200/1=100

1.2 概要

        当你不用实际尺寸,而需要用百分比描述可用空间时,使用LinearLayout的weight是非常重要的。如果你的目标是Honeycomb系统,并要使用Fragments,你会看到放置Fragments的大部分例程都使用了weight。理解如何使用weight将给你的工具箱里添加一个重要的工具。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I started learning about Android back in 2009. Android version 1.5 had just been released, and it showed a lot of potential. In July 2009, thanks to a friend living in Australia, I got my first Android-powered device, an HTC Magic with Android version 1.5. To be honest, it processed more slowly than I expected, but I started testing the API s and creating apps that I wanted to have on my cell phone. I sensed that Android would get a lot of attention and I knew that if I managed to create an application, it would be available to a lot of people. I was proved right—not long afterward, there was a kick-off for Android develop- ment, which soon grew bigger and bigger. Suddenly a lot of tools and third-party libraries supporting the Android platform emerged—everything from game frame- works, like cocos2d-x, to build systems, like Apache Maven. In November 2010 I was asked to review a book from Manning Publications called Android in Practice (www.manning.com/collins/). Delving deep into Manning’s work, it occurred to me that I could write a book about Android development using a differ- ent approach. I wanted to imitate Joshua Bloch’s Effective Java (www.amazon.com/ Effective-Java-2nd-Joshua-Bloch/dp/0321356683), providing tips and patterns I had learned over all my years of developing for the Android platform. Essentially, I wanted to gather together in one book every Android tip I have learned and provide some degree of documentation for it. That’s what 50 Android Hacks is all about: a collection of tips gathered in the process of developing different Android applications. Something I enjoyed about Effective Java was that the book doesn’t have any partic- ular order and I could read various sections, learning something different from each of them. After some time, I would go back to the book and find a different application for the project I was working on. I kept that in mind while writing this book. I imagine the reader investigating a hack while going to work or before going to sleep, getting new ideas for the project they’re working on. I’m already using this book on my new projects, copying the sample code for cer- tain tasks and using its examples to explain to my coworkers certain patterns. It’s proven to be useful for myself, and I hope it will be useful for you as well. While writing the book and samples, I set the minimum SDK to 1.6. Most of the hacks in the book work in Android version 1.6 onward unless mentioned. You’ll notice that there are hacks specific to the newest Android versions, but most of them are recommendations or ideas that would work for every version. Every hack has an icon identifying the minimum SDK it will work with. So pick a hack of interest to you from the table of contents and start reading. I hope you learn as much reading this book as I learned writing it.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值