zoukankan      html  css  js  c++  java
  • Android学习第五天

    详解3种基本布局

    LinearLayout又称作线性布局,这个布局会将它所包含的控件在线性方向上依次排列。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2" />
    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3" />
    </LinearLayout>

     LinearLayout横向排列的效果。

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

    LinearLayout使用ayout_gravity属性对齐的效果。
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:text="Button 1" />
    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Button 2" />
    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="Button 3" />
    </LinearLayout>

    RelativeLayout又称作相对布局,它可以通过相对定位的方式让控件出现在布局的任何位置。
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" …>
    <Button …
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button 1" />
    <Button …
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button 2" />
    <Button …
    android:layout_centerInParent="true"
    android:text="Button 3" />
    <Button …
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button 4" />
    <Button …
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Button 5" />
    </RelativeLayout>

    除了相对于父布局定位之外,RelativeLayout还允许相对于控件进行定位。
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...>
    <Button ...
    android:layout_centerInParent="true"
    android:text="Button 3" />
    <Button ...
    android:layout_above="@id/button3"
    android:layout_toLeftOf="@id/button3"
    android:text="Button 1" />
    <Button ...
    android:layout_above="@id/button3"
    android:layout_toRightOf="@id/button3"
    android:text="Button 2" />
    <Button ...
    android:layout_below="@id/button3"
    android:layout_toLeftOf="@id/button3"
    android:text="Button 4" />
    <Button ...
    android:layout_below="@id/button3"
    android:layout_toRightOf="@id/button3"
    android:text="Button 5" />
    </RelativeLayout>

  • 相关阅读:
    五分钟快速搭建Serverless免费邮件服务
    【Python基础编程240 ● 异常 ● 异常的跨函数传递】
    【Python基础编程239 ● 异常 ● 异常语句中else语句的使用】
    【Python基础编程238 ● 异常 ● 异常语句中else语句的使用】
    【Python基础编程237 ● 异常 ● 异常处理的基本格式:单独捕获单独处理】
    【Python基础编程236 ● 异常 ● 异常处理的基本格式】
    【Python基础编程235 ● 异常 ● 异常处理的基本格式】
    【Python基础编程234 ● 异常 ● 异常的概述】
    【Python基础编程233 ● 面向对象 ● 多态】
    【Python基础编程232 ● 面向对象 ● 静态方法】
  • 原文地址:https://www.cnblogs.com/yongyuandishen/p/14865984.html
Copyright © 2011-2022 走看看