Android基础知识及线性布局介绍

 更新时间:2022年01月14日 10:32:18   作者:老实东  
大家好,本篇文章主要讲的是Android基础知识及线性布局介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

1.常见控件的基本属性

android:id="@+id/button1":【设置控件id】

android:layout_width【设置控件宽度】/android:layout_height【设置控件高度】

wrap_content【控件的大小由内部决定】

match_parent【控件的大小与父控件保持一致】

android:text=" ":【设置组件文本】

android:textColor=" ":【设置字体颜色】

android:layout_marginLeft:【当前布局与父布局左边缘的距离】

android:layout_marginRight:【当前布局与父布局右边缘的距离】

android:layout_marginTop:【当前布局与父布局顶部边缘的距离】

android:layout_marginBottom:【当前布局与父布局底部边缘的距离】

android:gravity :【view里面的内容在这个view中的位置】

android:layout_gravity :【这个view相对于它父view的位置】

1、gravity在线性布局中不起任何作用,layout_gravity在线性布局中起作用;
2、 当我们使用 android:orientation=“vertical” 时, android:layout_gravity只有水平方向的设置才起作用,
垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的;
3、当 我们使用android:orientation=“horizontal” 时, android:layout_gravity只有垂直方向的设置才起作用,
水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

1.1控件的可见性

该属性有三种状态值:gone、visible、invisible。

gone 与invisible的区别是:
gone 表示控件不可见,也不会占任何的位置,也不会有任何响应。
而invisible表示控件虽然不可见,但是会占据它的宽高位置。

例子:

<LinearLayout
      android:id="@+id/linearLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">

      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="button1">

      </Button>

      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:visibility="invisible"      //invisible表示控件虽然不可见,但是会占据它的宽高位置。
          android:text="button2">
      </Button>

      <Button
          android:id="@+id/button3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"

          android:text="button3"></Button>

  </LinearLayout>

效果如图:

在这里插入图片描述

例子:

<LinearLayout
      android:id="@+id/linearLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">

      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="button1">

      </Button>

      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:visibility="gone"     //gone 表示控件不可见,也不会占任何的位置,也不会有任何响应。
          android:text="button2">
      </Button>

      <Button
          android:id="@+id/button3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="button3">

      </Button>

  </LinearLayout>

效果如图:

在这里插入图片描述

1.2控件的外边距

学习过HTML的都会知道CSS里的盒模式有个外边距和内边距。
外边距可以设置视图距离父视图上下左右的距离。
内边距可以设置视图内部内容距离自己边框上下左右的距离。
Android 的控件布局其实也用的是这个盒模式。

如果距离父视图上下左右的外边距相同,可以这么设置:

android:layout_margin="10dp"

我们也可以单独的设置某个外边距:

android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"

1.3控件的内边距

统一设置上下左右内边距:

android:padding="5dp"

各自设置内边距:

android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"

2.线性布局(Linear Layout)

LinearLayout 核心属性:
​ (1) android:orientation:两个属性值:“vertical” 垂直 “horizontal”水平
​ (2) android:layout_weight 将父控件的剩余空间按照设置的权重比例再分配

2.1示例:

在这里插入图片描述

<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="button1">

        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:text="button2">
        </Button>

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="button3">

        </Button>

    </LinearLayout>

2.2微信界面实战

在这里插入图片描述

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="×"
        android:textSize="50dp"
        android:layout_marginLeft="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="5dp"
        android:text="微信号/QQ/邮箱登录"
        android:textColor="@color/black"
        android:textSize="30dp"/>


<!--第一个框架-->


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="6dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:text="账号"
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0">
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="请填写微信号/QQ号/邮箱                  "/>
        </LinearLayout>

    </LinearLayout>

<!--第二个框架-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:text="密码"
                android:textColor="@color/black"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请填写密码                                          "/>

        </LinearLayout>

    </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用手机号登录"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="25dp"
            android:textSize="20dp"
            android:textColor="@color/purple_500"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        />
<!--    第三个框架-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="找回密码"
                android:layout_marginLeft="80dp"
                android:textColor="@color/purple_500"
                android:textSize="15dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="122dp"
            android:layout_height="wrap_content"
            android:layout_weight="7">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="紧急冻结"
                android:layout_marginLeft="40dp"
                android:textColor="@color/purple_500"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="70">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="微信安全中心"
                android:textColor="@color/purple_500"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

3.总结

到此这篇关于Android基础知识及线性布局介绍的文章就介绍到这了,更多相关Android线性布局内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android通过反射实现强制停止应用程序的方法

    Android通过反射实现强制停止应用程序的方法

    这篇文章主要介绍了Android通过反射实现强制停止应用程序的方法,涉及Android的反射机制与进程操作的相关技巧,需要的朋友可以参考下
    2016-02-02
  • Android布局技巧之include、merge与ViewStub标签的巧用

    Android布局技巧之include、merge与ViewStub标签的巧用

    Android 官方提供了三个用来优化布局的标签,分别是include、merge与ViewStub,下面这篇文章主要给大家介绍了关于Android布局技巧之include、merge与ViewStub标签巧用的相关资料,需要的朋友可以参考下
    2018-06-06
  • AndroidStudio4.0日志中文乱码问题

    AndroidStudio4.0日志中文乱码问题

    这篇文章主要介绍了AndroidStudio4.0日志中文乱码问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Android实现获取联系人电话号码功能

    Android实现获取联系人电话号码功能

    这篇文章主要为大家详细介绍了Android实现获取联系人电话号码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android 修改adb端口的方法

    Android 修改adb端口的方法

    今天小编就为大家分享一篇Android 修改adb端口的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Android内容提供者ContentProvider用法实例分析

    Android内容提供者ContentProvider用法实例分析

    这篇文章主要介绍了Android内容提供者ContentProvider用法,结合实例形式较为详细的分析了内容提供者ContentProvider获取及解析数据的相关技巧,需要的朋友可以参考下
    2016-03-03
  • Android开发欢迎页点击跳过倒计时进入主页

    Android开发欢迎页点击跳过倒计时进入主页

    没点击跳过自然进入主页,点击跳过之后立即进入主页,这个功能怎么实现呢,本文通过实例代码给大家介绍Android开发欢迎页点击跳过倒计时进入主页,感兴趣的朋友一起看看吧
    2023-12-12
  • android实现加载动画对话框

    android实现加载动画对话框

    这篇文章主要为大家详细介绍了android实现加载动画对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • Android百度地图定位后获取周边位置的实现代码

    Android百度地图定位后获取周边位置的实现代码

    这篇文章主要为大家详细介绍了Android百度地图定位后获取周边位置的实现代码,准确获取周边地理位置,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android教你如何发现APP卡顿的实现

    Android教你如何发现APP卡顿的实现

    这篇文章主要介绍了Android教你如何发现APP卡顿的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论