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

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

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

猜你喜欢

转载自blog.csdn.net/qq_43227109/article/details/83185982
今日推荐