4安卓的简单布局

    天下皆知美之为美,恶已;皆知善,斯不善矣。有无之相生也,难易之相成也,长短之相刑,高下之相盈也,音声之相和也,先后之相随,恒也。是以圣人居无为之事,行不言之教,万物作而弗始也,为而弗志也,成功而弗居也。夫唯弗居,是以弗去                        ----《道德经-第二章》

天下人都知道美之所以为美,那是由于有丑陋的存在。都知道善之所以为善,那是因为有恶的存在。所以有和无互相转化,难和易互相形成,长和短互相显现,高和下互相充实,音与声互相谐和,前和后互相接随——这是永恒的。因此圣人用无为的观点对待世事,用不言的方式施行教化:听任万物自然兴起而不为其创始,有所施为,但不加自己的倾向,功成业就而不自居。正由于不居功,就无所谓失去。


1.LinearLayout布局

线性布局有两种方式

从左到右 :android:orientation=”horizontal”

从上到下 :android:orientation=”vertical” 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#157109" />

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#48EF30" />

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#93F585" />

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#CBFAC5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="96dp"
            android:background="#157109" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="96dp"
            android:background="#48EF30" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="96dp"
            android:background="#93F585" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="96dp"
            android:background="#CBFAC5" />
    </LinearLayout>

</LinearLayout>
效果如下


2.RelativeLayout布局

 

参考其他控件进行布局,默认为父控件。 
有三种类型的属性:

· 属性值是true或false 

· android:layout_centerHrizontal 水平居中

· android:layout_centerVertical 垂直居中

· android:layout_centerInparent 相对于父元素完全居中。

· android:layout_alignParentBottom 位于父元素的下边缘

· android:layout_alignParentTop 位于父元素的上边缘

· android:layout_alignParentLeft 位于父元素的左边缘

· android:layout_alignParentRight 位于父元素的右边缘

· 属性值是”@id/*“ 

· android:layout_below 在某元素的下方

· android:layout_above 在某元素的上方

· andorid:layout_toRightOf 在某元素的右方

· android:layout_toLeftOf 在某元素的左方

· android:layout_alignBottom 和某元素下方对齐

· android:layout_alignTop 和某元素上方对齐

· android:layout_alignRight 和某元素右方对齐

· android:layout_alignLeft 和某元素左方对齐

· 属性值是数值 

· android:layout_marginLeft 离某元素左边缘的距离

· android:layout_marginRight 离某元素右边缘的距离

· android:layout_marginTop 离某元素上边缘的距离

· android:layout_marginBottom 离某元素下边缘的距离

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
   <TextView
       android:id="@+id/text1"
       android:layout_width="50dp"
       android:background="#008000"
       android:layout_height="50dp"
       android:text="1"
       android:textAlignment="center"
       android:textSize="25dp"
       android:textColor="#FFFFFF"
       />

    <TextView
        android:id="@+id/text2"
        android:layout_width="50dp"
        android:background="#FF8000"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/text1"
        android:text="2"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"
        />

    <TextView
        android:id="@+id/text3"
        android:layout_width="50dp"
        android:background="#FF8000"
        android:layout_height="50dp"
        android:layout_below="@id/text2"
        android:text="3"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"
        />
    <TextView
        android:id="@+id/text4"
        android:layout_width="50dp"
        android:background="#FF8000"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:text="4"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"
        />
    <TextView
        android:id="@+id/text5"
        android:layout_width="50dp"
        android:background="#4234"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:text="5"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"

        />
    <TextView
        android:id="@+id/text6"
        android:layout_width="50dp"
        android:background="#4234"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:text="6"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"

        />
    <TextView
        android:id="@+id/text7"
        android:layout_width="50dp"
        android:background="#4234"
        android:layout_height="50dp"
        android:layout_below="@id/text3"
        android:text="7"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"

        />
    <TextView
        android:id="@+id/text8"
        android:layout_width="50dp"
        android:background="#4234"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/text4"
        android:text="8"
        android:textAlignment="center"
        android:textSize="25dp"
        android:textColor="#FFFFFF"

        />

</RelativeLayout>

效果如下




3.TableLayout布局

 

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TableRow>
        <Button android:text="1"/>
        <Button android:text="1"/>
        <Button android:text="1"/>
    </TableRow>
    <TableRow>
        <Button android:text="1"/>
        <Button android:text="1"/>
        <Button android:text="1"/>
    </TableRow>
    <TableRow>
        <Button android:text="1"/>
        <Button android:text="1"/>
        <Button android:text="1"/>
    </TableRow>
</TableLayout>

 效果如下






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款由Google开发的集成开发环境(IDE),用于开发Android应用程序。它提供了丰富的功能和工具,可以帮助开发者轻松地创建、编译、调试和发布Android应用。 在Android Studio中,布局是指定义应用程序界面的方式。Android Studio提供了多种布局方式,其中最常用的是简易布局,也称为线性布局(LinearLayout)。 简易布局是一种按照线性方向排列子视布局方式。可以水平排列子视,也可以垂直排列子视。以下是使用简易布局的步骤: 1. 打开Android Studio并创建一个新的Android项目。 2. 在项目的res/layout目录下找到activity_main.xml文件,这是应用程序的主布局文件。 3. 在activity_main.xml文件中,使用LinearLayout标签作为根布局,并设置其orientation属性为水平或垂直,以确定子视的排列方向。 4. 在LinearLayout标签内部,添加其他视作为子视。可以使用各种视组件,如TextView、Button、ImageView等。 5. 使用layout_width和layout_height属性来设置子视的宽度和高度。 6. 使用layout_weight属性来设置子视布局中的权重,以实现灵活的布局。 以下是一个简单的示例代码: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> </LinearLayout> ``` 这个示例代码创建了一个垂直排列的简易布局,包含一个TextView和一个Button。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值