Menu Home

Android Activity布局(layout)介绍

LinearLayout

LinearLayout——线性布局,顾名思义,LinearLayout中的组件按照线性排列,每一个组件都位于前一个组件的后面。LinearLayout又可以分为水平线性布局和垂直线性布局。

垂直线性布局

在垂直线性布局中,组件从上至下依次排列。可以使用android:layout_weight属性来控制组件占据空间的比例:当layout_width或layout_height设置为0dp的时候,数值越高占据的比例越大;当layout_width或layout_height设置为match_parent时,数值越高占据的比例越小。

1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="TextView 1" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView 2" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView 3" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView 4" />

水平线性布局

水平线性布局中的组件按照水平方向平铺排列,组件所占空间的比例跟layout_weight的值成正比。

2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TV 1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="TV 2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TV 3" />

&lt/LinearLayout>

RelativeLayout

RelativeLayout——相对布局。在相对布局中,组件的位置关系是相对的,我们可以用下面的几个属性来控制组件之间的相对关系:

属性名 相对位置
layout_toLeftOf 位于指定组件的左侧
layout_toRightOf 位于指定组件的右侧
layout_above 位于指定组件的上方
layout_below 位于指定组件的下方
layout_alignParentLeft 与父组件左对齐
layout_alignParentRight 与父组件右对齐
layout_alignParentTop 与父组件上对齐
layout_alignParentBottom 与父组件下对齐
layout_centerInParent 与父组件居中对齐
layout_centerHorizontal 垂直居中
layout_centerVertica 水平居中

3

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/tv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff00ffff"
        android:gravity="center"
        android:text="1" />

    <Button
        android:id="@+id/tv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/tv1"
        android:background="#ffffff00"
        android:gravity="center"
        android:text="2" />

    <Button
        android:id="@+id/tv3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/tv2"
        android:background="#ffff00ff"
        android:gravity="center"
        android:text="3" />

    <Button
        android:id="@+id/tv4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/tv2"
        android:layout_toRightOf="@+id/tv3"
        android:background="#ffff0000"
        android:gravity="center"
        android:text="4" />

</RelativeLayout>

FrameLayout

FrameLayout——帧布局。帧布局中的所有组件都放在布局的左上角,所有组件会层叠在一起。下面的例子展示的是一个相机应用,这个应用的布局中有一个按钮和一个文本框飘浮在一个SurfaceView之上,这样我们既可以做到全屏预览相机画面,又可以把拍照按钮和一些信息显示在预览画面之上。

Screenshot_2013-10-03-15-22-03

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/FL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <SurfaceView
            android:id="@+id/CV"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <LinearLayout
            android:id="@+id/LL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/TV"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="10"
                android:textSize="100dip" />

            <Button
                android:id="@+id/BT"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Take Picture" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

12 replies

  1. My wife and i have been now peaceful when Chris managed to finish off his basic research because of the ideas he acquired while using the web pages. It’s not at all simplistic to just continually be giving for free helpful hints which usually some other people could have been selling. And we do know we now have you to thank for that. Those illustrations you made, the simple site menu, the relationships your site give support to instill – it’s got most amazing, and it’s really leading our son in addition to us feel that that topic is enjoyable, and that’s pretty essential. Thanks for all!

  2. Thanks a lot for giving everyone an extremely remarkable chance to read in detail from this web site. It is always very amazing and jam-packed with a good time for me and my office acquaintances to visit your website at a minimum thrice in a week to find out the newest items you have. And indeed, I am usually fulfilled with all the staggering hints you give. Certain 1 ideas on this page are definitely the very best I have ever had.

  3. Thank you for your own labor on this web page. Gloria take interest in working on internet research and it’s really obvious why. We all learn all regarding the dynamic method you give invaluable thoughts via the website and in addition invigorate contribution from other ones about this theme and our girl is learning a lot of things. Take pleasure in the rest of the year. You’re the one performing a good job.

  4. I intended to post you that bit of note to finally say thank you again over the stunning advice you have contributed at this time. It has been really incredibly open-handed with people like you to provide unhampered just what many of us would have offered for an e book to earn some profit for themselves, principally considering the fact that you might well have done it in case you considered necessary. These smart ideas likewise worked to provide a good way to fully grasp that many people have the identical dreams much like mine to learn whole lot more with reference to this matter. I think there are many more enjoyable instances ahead for people who discover your site.

  5. I simply wanted to send a simple word to be able to express gratitude to you for these precious strategies you are placing on this site. My time intensive internet investigation has at the end of the day been paid with high-quality tips to write about with my guests. I ‘d point out that we visitors are unquestionably blessed to exist in a remarkable network with so many special people with beneficial basics. I feel quite lucky to have used your web page and look forward to really more excellent times reading here. Thanks a lot once again for all the details.

  6. I intended to send you a little bit of remark just to thank you so much once again just for the wonderful tricks you’ve discussed in this article. It has been so surprisingly generous with you to give without restraint what exactly many people might have advertised for an electronic book to make some profit for themselves, primarily considering that you might well have tried it if you ever considered necessary. Those principles additionally served to become fantastic way to know that other people have a similar keenness just as mine to know the truth significantly more related to this problem. I’m certain there are a lot more pleasurable opportunities in the future for individuals that look over your blog post.

  7. I am also writing to make you know of the fantastic experience my friend’s girl enjoyed checking yuor web blog. She noticed a good number of issues, most notably how it is like to possess a wonderful helping heart to make the mediocre ones just comprehend certain problematic subject matter. You undoubtedly did more than our desires. Thank you for rendering those necessary, healthy, revealing and cool thoughts on your topic to Tanya.

  8. My spouse and i got really peaceful when Jordan could deal with his homework through your ideas he had while using the web pages. It’s not at all simplistic to simply happen to be releasing guides that many other folks might have been making money from. And now we know we have you to thank for this. The specific illustrations you made, the easy web site navigation, the relationships your site help foster – it’s got all awesome, and it is facilitating our son in addition to our family reckon that that content is amusing, which is certainly particularly vital. Thanks for the whole thing!

  9. I precisely desired to thank you so much yet again. I am not sure the things that I could possibly have made to happen without the type of strategies documented by you over my field. It had been an absolute horrifying scenario in my opinion, nevertheless observing your well-written manner you treated that made me to leap for gladness. Extremely happy for your work as well as wish you find out what an amazing job your are accomplishing teaching other individuals using your webpage. Most likely you’ve never got to know all of us.

  10. I in addition to my guys came analyzing the great information and facts on your web page and so unexpectedly developed an awful feeling I had not expressed respect to the website owner for them. These guys happened to be as a result glad to read them and have in effect absolutely been taking pleasure in them. Thank you for getting so kind and also for considering this sort of cool things millions of individuals are really desirous to understand about. My very own sincere apologies for not saying thanks to earlier.

  11. I simply had to say thanks yet again. I do not know what I might have accomplished without the entire smart ideas provided by you on this field. Previously it was a frightening crisis in my view, however , coming across a expert fashion you dealt with it made me to leap for delight. I’m grateful for this advice as well as wish you know what a powerful job you were providing teaching most people through your web site. Most likely you’ve never got to know any of us.

  12. I just wanted to compose a brief word so as to thank you for all the stunning tips you are giving out at this website. My time consuming internet lookup has at the end been rewarded with awesome strategies to exchange with my family and friends. I ‘d point out that most of us site visitors are undoubtedly fortunate to be in a useful place with so many wonderful people with valuable advice. I feel truly grateful to have encountered the web pages and look forward to really more brilliant times reading here. Thank you again for a lot of things.

Leave a Reply

Your email address will not be published.