《android开发必知的50个诀窍》笔记Hack-1

问题:如何将一个按钮居中显示,并占据去父视图宽度的一般?

解决方案:合用weightSum属性和layout_weight属性,具体代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="Button" />
</LinearLayout>

效果图如下:

宽度占父视图的一半

分析:

LinearLayout里面的weightSum属性值,表示其内部所有子视图的weight比例总和,此时设置为1,而Button的layout_weight设置为0.5恰好为其一半,故占据起宽度的二分之一。

扩展

子控件的宽度(根据android:orientation的值会不同,或者是高度)计算结果为:
子控件的width+(weight/weitSum)*(父视图的width-所有子视图的width)

  • 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、付费专栏及课程。

余额充值