android开发分享Android基础知识及线性布局介绍

目录1.常见控件的基本属性1.1控件的可见性1.2控件的外边距1.3控件的内边距2.线性布局(linear layout)2.1示例:2.2微信界面实战3.总结1.常见控件的基本属性android:i

目录
  • 1.常见控件的基本属性
    • 1.1控件的可见性
    • 1.2控件的外边距
    • 1.3控件的内边距
  • 2.线性布局(linear layout)
    • 2.1示例:
    • 2.2微信界面实战
  • 3.总结

    上述就是android开发分享Android基础知识及线性布局介绍的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

    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>  

    效果如图:

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

    例子:

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

    效果如图:

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

    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示例:

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

    <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微信界面实战

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

    <?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="https://schemas.android.com/apk/res/android"      xmlns:app="https://schemas.android.com/apk/res-auto"      xmlns:tools="https://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线性布局内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

    本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

    ctvol管理联系方式QQ:251552304

    本文章地址:https://www.ctvol.com/addevelopment/1033274.html

    (0)
    上一篇 2022年1月18日
    下一篇 2022年1月18日

    精彩推荐