Android布局1(线性布局跟帧布局)

本文深入讲解了Android应用中布局设计的核心概念,包括线性布局、帧布局的特点与使用技巧,探讨了layout_weight属性的正确应用,以及如何通过XML配置文件进行界面设计。

1. 什么是布局

 就是把界面中的控件按照某种规律摆放到指定的位置。

2. 布局的二种实现

 代码
   xml配置文件:res/layout目录下
     注:也可以同时使用xml和代码

首先用线性布局写一个案例

 orientation="vertical|horizontal"//设置是垂直布局还是水平布局
   android:gravity:控件内部的元素(对内有效)

假如写一个下面居中的列子:gravity="bottom|center"中间是并且 等等等等还有很多的属性

父元素指整个Activity屏幕 用:gravity 父元素指定垂直 子元素只能左右

子指里面的那些一个个控件 用:layout-gravity

<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">

    <!--1. 使用线性布局完成界面效果1
     提示:线性布局嵌套+layout_weight-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"//比重占屏幕的多少
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:layout_gravity="left"//到左边
            android:text="按钮1" />
<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginLeft="200dp"//设置离左边那个多远
            android:text="按钮2" />
    </LinearLayout>
一部分代码

4:LinearLayout布局中Layout_weight属性的作用

4.1 首先声明只有在Linearlayout中,layout_weight属性才有效。
  4.2 它是用来指定(剩余空闲空间)的分割比例,而非按比例分配整个空间。 
  4.3 另外android:layout_weight会引起争议,是因为在设置该属性的同时,
      设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。
  4.4 如果想用layout_weight平均分配空间,正确方式是将layout_width(或layout_height)设置为0dp,
      再通过layout_weight按比例分配空间
  注:关键点在于搞清楚什么是剩余空闲空间

 

5.帧布局(就好象一张张卡片堆叠上去,后面会盖出前面的)
    FrameLayout
    注:帧布局有点类似于awt的CardLayout都是把组件一个一个叠加到一起,
        但CardLayout能将下面的组件移上来,但FrameLayout没有提供此功能

<FrameLayout 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:layout_margin="20dp"//离外边距多少距离
    android:background="@color/blue"
    tools:context=".MainActivity">

    <TextView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/yellow"//设置颜色
        android:layout_margin="20dp"//设置外边距多少距离
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/green"
        android:layout_margin="40dp"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

这两个布局暂时自己了解就介绍到这么多了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值