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 Service中使用Toast无法正常显示问题的解决方法

    Android Service中使用Toast无法正常显示问题的解决方法

    这篇文章主要介绍了Android Service中使用Toast无法正常显示问题的解决方法,分析了Service中Toast无法正常显示的原因与相关的解决方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • android Activity相对布局的使用方法

    android Activity相对布局的使用方法

    Activity相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,这些位置都是相对的,确定出了其中一个控件的位置就可以确定另一个控件的位置,下面用实例说明Activity相对布局的使用方法
    2013-11-11
  • Kotlin StateFlow单数据更新热流设计与使用介绍

    Kotlin StateFlow单数据更新热流设计与使用介绍

    StateFlow当值发生变化,就会将值发送出去,下流就可以接收到新值。在某些场景下,StateFlow比LiveData更适用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-09-09
  • 微信小程序 bnner滚动实例详解

    微信小程序 bnner滚动实例详解

    这篇文章主要介绍了微信小程序 bnner滚动实例详解的相关资料,需要的朋友可以参考下
    2017-01-01
  • android获取手机唯一标识的方法

    android获取手机唯一标识的方法

    这篇文章主要介绍了获取安卓的手机或者平板的唯一标识的方法,需要的朋友可以参考下
    2014-02-02
  • 解决PhoneGap不支持viewport的几种方法

    解决PhoneGap不支持viewport的几种方法

    今天小编就为大家分享一篇关于解决PhoneGap不支持viewport的几种方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android实现简单MD5加密的方法

    Android实现简单MD5加密的方法

    这篇文章主要介绍了Android实现简单MD5加密的方法,涉及Android编码操作及加密的相关实现技巧,需要的朋友可以参考下
    2016-08-08
  • 完美解决Android Studio集成crashlytics后无法编译的问题

    完美解决Android Studio集成crashlytics后无法编译的问题

    下面小编就为大家带来一篇完美解决Android Studio集成crashlytics后无法编译的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Android 后台发送邮件示例 (收集应用异常信息+Demo代码)

    Android 后台发送邮件示例 (收集应用异常信息+Demo代码)

    今天介绍个更简单的方法,我们把异常信息收集后,通过后台发送邮件方法,把相关异常信息发送到我们指定的邮箱里面
    2013-07-07
  • 浅谈Flutter解析JSON三种方式

    浅谈Flutter解析JSON三种方式

    这篇文章主要介绍了浅谈Flutter解析JSON三种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论