2021-06-10Android布局

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


Android布局

在 Android APP 项目中,有数种布局方式:线性布局、相对布局、扁平化布局、绝对布局等。用的最多的就是线性布局和相对布局,由于 Android 手机分辨率的定制化严重,没有统一的分辨率标准,绝对布局使用最少。


一、LinearLayout

1.格式

<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"
    tools:context=".MainActivity">

	<TextView
	....
	/>
	....控件,布局等

</LinearLayout>

2.其他属性

排列方式
纵向:android:orientation=“vertical”
横向:android:orientation=“horizontal”

分割线属性
android:divider 将控件之间隔开

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- LinearLayout 线性布局-->
<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"
    tools:context=".MainActivity">

    <!--    android:orientation="" 横向/线性排列,只有线性布局才有-->

    <!--    margin演示-->
    <LinearLayout
        android:id="@+id/LL_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ff0000"
        android:orientation="horizontal">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="#00FF00" />

        <!--        android:layout_margin="" 外边距-->

    </LinearLayout>

    <!--    权重演示-->
    <LinearLayout
        android:id="@+id/LL_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:background="#000000"
        android:orientation="horizontal">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#ffffff" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#0000ff" />

    </LinearLayout>

    <!--    居中演示-->
    <!--    权重演示-->
    <LinearLayout
        android:id="@+id/LL_3"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="15dp"
        android:background="#000000"
        android:baselineAligned="false"
        >

        <!--        水平并垂直居中-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            >

            <!--            android:gravity="" 用于居中子控件-->

            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="0dp"
                android:background="#0000ff"
                />

        </LinearLayout>

        <!--        水平居中-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#0000ff"
            android:gravity="center_horizontal"
            >
            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="0dp"
                android:background="#ffffff"
                />
        </LinearLayout>

        <!--        垂直居中-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#0000ff"
            android:gravity="center_vertical"
            >
            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="0dp"
                android:background="#ffffff"
                />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

二、RelativeLayout

相对布局

<RelativeLayout></RelativeLayout>

例子:

<?xml version="1.0" encoding="utf-8"?>
<!-- RelativeLayout 相对布局-->
<RelativeLayout 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"
    tools:context=".MainActivity">

    <!--    android:orientation="" 横向/线性排列,只有线性布局才有,相对布局没有-->

    <!--    margin演示-->
    <RelativeLayout
        android:id="@+id/LL_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ff0000">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="#00FF00" />

        <!--        android:layout_margin="" 外边距-->

    </RelativeLayout>

    <!--    权重演示-->
    <LinearLayout
        android:id="@+id/LL_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/LL_1"
        android:layout_marginTop="10dp"
        android:background="#000000">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#ffffff" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:background="#0000ff" />

    </LinearLayout>

    <!--    居中演示-->
    <LinearLayout
        android:id="@+id/LL_3"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/LL_2"
        android:layout_marginTop="15dp"
        android:background="#000000"
        android:baselineAligned="false">

        <!--        水平并垂直居中-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:background="#ffffff"
            android:layout_weight="1"
            android:gravity="center">

            <!--            android:gravity="" 用于居中子控件-->

            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="0dp"
                android:background="#0000ff" />

        </LinearLayout>

        <!--        水平居中-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:background="#0000ff"
            android:layout_weight="1"
            android:gravity="center_horizontal">

            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="0dp"
                android:background="#ffffff" />
        </LinearLayout>

        <!--        垂直居中-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:background="#0000ff"
            android:layout_weight="1"
            android:gravity="center_vertical">

            <View
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="0dp"
                android:background="#ffffff" />
        </LinearLayout>

    </LinearLayout>

    <!--    对齐演示-->
    <RelativeLayout
        android:id="@+id/LL_4"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:layout_below="@id/LL_3"
        android:layout_marginTop="15dp"
        android:background="#000000"
        >

        <RelativeLayout
            android:id="@+id/LL_4_1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:background="#00ff00"
            >

        </RelativeLayout>

        <!--        android:layout_toRightOf 对齐目标图像右边界-->
        <RelativeLayout
            android:id="@+id/LL_4_2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:background="#00ffff"
            android:layout_toRightOf="@id/LL_4_1"
            >

        </RelativeLayout>

        <!--        android:layout_alignParentTop 对齐父控件上边界-->
        <RelativeLayout
            android:id="@+id/LL_4_3"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_margin="15dp"
            android:background="#00ffff"
            android:layout_toRightOf="@id/LL_4_2"
            android:layout_alignParentTop="true"
            >


        </RelativeLayout>

        <!--        android:layout_below 对齐目标下边界-->
        <RelativeLayout
            android:id="@+id/LL_4_4"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_margin="15dp"
            android:background="#00ffff"
            android:layout_below="@id/LL_4_3"
            android:layout_toRightOf="@id/LL_4_2"
            >

        </RelativeLayout>

        <!--        android:layout_alignParentBottom 对齐父控件下边界-->
        <RelativeLayout
            android:id="@+id/LL_4_5"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_margin="15dp"
            android:background="#00ffff"
            android:layout_toRightOf="@id/LL_4_2"
            android:layout_alignParentBottom="true"
            >

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

三、FrameLayout

这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式,所以它应用的场景并不多;帧布局的大小由控件中最大的子控件决定,如果控件的大小一样大的话,那么同一时刻就只能看到最上面的那个组件!后续添加的控件会覆盖前一个!虽然默认会将控件放置在左上角,但是我们也可以通过layout_gravity属性,指定到其他的位置

常用属性

FrameLayout的属性很少就两个,但是在说之前我们先介绍一个东西:
前景图像:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。
两个属性:
android:foreground:*设置改帧布局容器的前景图像
android:foregroundGravity:设置前景图像显示的位置

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/FrameLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    tools:context=".MainActivity"     
    android:foreground="@drawable/logo"    
    android:foregroundGravity="right|bottom">    
    
    <TextView    
        android:layout_width="200dp"    
        android:layout_height="200dp"    
        android:background="#FF6143" />    
    <TextView    
        android:layout_width="150dp"    
        android:layout_height="150dp"    
        android:background="#7BFE00" />    
     <TextView    
        android:layout_width="100dp"    
        android:layout_height="100dp"    
        android:background="#FFFF00" />    
        
</FrameLayout> 

四、ConstraintLayout

五、Adapter

//

六、ListView

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
  private String data[] = {"马","牛","猪","兔","鼠","狗","猪","aa","bb","cc","dd","aa","bb","cc","dd"};//假数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listview);//在视图中找到ListView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);//新建并配置ArrayAapeter
        listView.setAdapter(adapter);
    }

添加监听方法

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                    case 0:
                        Toast.makeText(MainActivity.this,"你点击了"+i+"按钮",Toast.LENGTH_SHORT).show();
                        break;//当我们点击某一项就能吐司我们点了哪一项
 
                    case 1:
                        Toast.makeText(MainActivity.this,"你点击了"+i+"按钮",Toast.LENGTH_SHORT).show();
                        break;
 
                    case 2:
                        Toast.makeText(MainActivity.this,"你点击了"+i+"按钮",Toast.LENGTH_SHORT).show();
                        break;
 
                    case 3:
                        Toast.makeText(MainActivity.this,"你点击了"+i+"按钮",Toast.LENGTH_SHORT).show();
                        break;
 
                    case 4:
                        Toast.makeText(MainActivity.this,"你点击了"+i+"按钮",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

七、RecyclerView

示例


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值