欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

腾龙娱乐开户16624610555

程序员文章站 2022-05-15 09:07:53
...

目录
1.常见控件的基本属性
1.1控件的可见性
1.2控件的外边距
1.3控件的内边距
2.线性布局(Linear Layout)
2.1示例:
2.2微信界面实战
3.总结
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">

  1. <Button
  2. android:id="@+id/button1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_weight="1"
  6. android:text="button1">
  7. </Button>
  8. <Button
  9. android:id="@+id/button2"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:visibility="invisible" //invisible表示控件虽然不可见,但是会占据它的宽高位置。
  14. android:text="button2">
  15. </Button>
  16. <Button
  17. android:id="@+id/button3"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:text="button3"></Button>
  22. </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">

  1. <Button
  2. android:id="@+id/button1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_weight="1"
  6. android:text="button1">
  7. </Button>
  8. <Button
  9. android:id="@+id/button2"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:visibility="gone" //gone 表示控件不可见,也不会占任何的位置,也不会有任何响应。
  14. android:text="button2">
  15. </Button>
  16. <Button
  17. android:id="@+id/button3"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:text="button3">
  22. </Button>
  23. </LinearLayout>
  24. 效果如图:

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

  1. <Button
  2. android:id="@+id/button1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="button1">
  6. </Button>
  7. <Button
  8. android:id="@+id/button2"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="button2">
  12. </Button>
  13. <Button
  14. android:id="@+id/button3"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="button3">
  18. </Button>
  19. </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">

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="×"
  5. android:textSize="50dp"
  6. android:layout_marginLeft="5dp"/>
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginLeft="20dp"
  11. android:layout_marginTop="5dp"
  12. android:text="微信号/QQ/邮箱登录"
  13. android:textColor="@color/black"
  14. android:textSize="30dp"/>
  15. <!--第一个框架-->
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="30dp">
  20. <LinearLayout
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:layout_marginTop="6dp"
  25. android:orientation="horizontal">
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_marginLeft="25dp"
  30. android:text="账号"
  31. android:textColor="@color/black"
  32. android:textSize="25dp" />
  33. </LinearLayout>
  34. <LinearLayout
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="0">
  38. <EditText
  39. android:layout_width="wrap_content"
  40. android:layout_height="match_parent"
  41. android:text="请填写微信号/QQ号/邮箱 "/>
  42. </LinearLayout>
  43. </LinearLayout>
  44. <!--第二个框架-->
  45. <LinearLayout
  46. android:layout_width="match_parent"
  47. android:layout_height="45dp"
  48. android:layout_marginTop="10dp"
  49. android:orientation="horizontal">
  50. <LinearLayout
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_weight="1">
  54. <TextView
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_marginLeft="25dp"
  58. android:text="密码"
  59. android:textColor="@color/black"
  60. android:textSize="25dp" />
  61. </LinearLayout>
  62. <LinearLayout
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:layout_weight="0">
  66. <EditText
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:text="请填写密码 "/>
  70. </LinearLayout>
  71. </LinearLayout>
  72. <TextView
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:text="用手机号登录"
  76. android:layout_marginTop="20dp"
  77. android:layout_marginLeft="25dp"
  78. android:textSize="20dp"
  79. android:textColor="@color/purple_500"/>
  80. <Button
  81. android:layout_width="match_parent"
  82. android:layout_height="wrap_content"
  83. android:text="登录"
  84. android:textSize="30dp"
  85. android:layout_marginTop="30dp"
  86. />
  87. <!-- 第三个框架-->
  88. <LinearLayout
  89. android:layout_width="match_parent"
  90. android:layout_height="wrap_content"
  91. android:layout_marginTop="150dp"
  92. android:orientation="horizontal">
  93. <LinearLayout
  94. android:layout_width="wrap_content"
  95. android:layout_height="wrap_content"
  96. android:layout_weight="2">
  97. <TextView
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:text="找回密码"
  101. android:layout_marginLeft="80dp"
  102. android:textColor="@color/purple_500"
  103. android:textSize="15dp" />
  104. </LinearLayout>
  105. <LinearLayout
  106. android:layout_width="122dp"
  107. android:layout_height="wrap_content"
  108. android:layout_weight="7">
  109. <TextView
  110. android:layout_width="wrap_content"
  111. android:layout_height="wrap_content"
  112. android:text="紧急冻结"
  113. android:layout_marginLeft="40dp"
  114. android:textColor="@color/purple_500"
  115. android:textSize="15dp" />
  116. </LinearLayout>
  117. <LinearLayout
  118. android:layout_width="wrap_content"
  119. android:layout_height="wrap_content"
  120. android:layout_weight="70">
  121. <TextView
  122. android:layout_width="wrap_content"
  123. android:layout_height="wrap_content"
  124. android:text="微信安全中心"
  125. android:textColor="@color/purple_500"/>
  126. </LinearLayout>
  127. </LinearLayout>
  128. </LinearLayout>

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