每日记录(AndroidStudio入门基础(三)——基础布局)

发布时间 2023-06-04 00:36:24作者: 傲世小苦瓜

.LinearLayout
线性布局要灵活一些,在实际应用上也是最最最广泛的。

layout_width
layout_height
和相对布局一样的用法和属性值,我就不赘述了!

区别于RelativeLayout,LinearLayout就不要求每个组件都要设置自己的id了,但是最好还是设置一下,这是一个好习惯哦。

那么问题来了,我怎么设置两个组件是横着并排还是竖着并排呢??现在就隆重介绍线性布局的重要属性 orientation

取值有两种:vertical(垂直)和 horizontal(水平)

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>

<Button
android:id="@+id/button_1"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 2"
/>

<Button
android:id="@+id/button_3"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 3"
/>

</LinearLayout>

 

android:orientation="horizontal" 决定了容器里面的所有组件都绝对是水平排列的
!!!需要注意的就是,哪怕我的组件已经装不下了,也不会被挤到下一排,而是只显示一截,甚至完全不显示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:id="@+id/button_1"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 2"
/>

</LinearLayout>

 

android:orientation="vertical"决定了容器里的组件都是垂直排列,这就很好理解了。

线性布局还有一个重要的属性 layout_weight 取值一般是1、2、3...表示权重的大小,例如:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:id="@+id/button_1"
android:layout_width="200px"
android:layout_height="100px"
android:layout_weight="1"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_width="200px"
android:layout_height="100px"
android:layout_weight="2"
android:text="Button 2"
/>

</LinearLayout>

 

LinearLayout的强大就在于它是可以嵌套的,从而实现很多复杂的布局。

为了巩固你对它的认识,我出一个小小的题目,这是我的课程设计的一个页面,你会怎么设计这个布局呢??

 

我自己也再试了试:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#FFEA8E"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#98E0AD">

<ImageView
android:id="@+id/image_bg"
android:layout_width="match_parent"
android:layout_height="258dp"
android:background="#EDD3ED"/>

<LinearLayout
android:layout_below="@id/image_bg"
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#F8A1A4"
android:orientation="vertical">

<RadioGroup
android:id="@+id/rg_main"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:background="#C785C8"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_me"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="28dp"
android:background="#C00000"
android:button="@null"
android:gravity="center"
android:textSize="19sp" />

<RadioButton
android:id="@+id/rb_heart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:paddingRight="33dp"
android:layout_weight="1"
android:button="@null"
android:background="#7030A0"
android:gravity="center"
android:textSize="19sp" />

<RadioButton
android:id="@+id/rb_order"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00B0F0"
android:paddingRight="34dp"
android:button="@null"
android:gravity="center"
android:textSize="19sp" />
</RadioGroup>
</LinearLayout>


<LinearLayout
android:id="@+id/layout_mywork"
android:layout_below="@id/layout_button"
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="#6BD089"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="62dp"
android:textColor="#212122"
android:gravity="center"
android:background="#8389E0"
android:textSize="24sp"
/>

</LinearLayout>

<LinearLayout
android:id="@+id/layout_work1"
android:layout_below="@id/layout_mywork"
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingRight="6dp"
android:paddingLeft="6dp"
android:background="#C785C8"
android:layout_height="150dp">

<ImageView
android:padding="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="160dp"
android:background="#FBCFD0"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
/>

<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#FBCFD0"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:layout_height="160dp"
/>

<ImageView
android:id="@+id/work1"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#FBCFD0"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:layout_height="160dp"
/>

</LinearLayout>

<LinearLayout
android:id="@+id/layout_work2"
android:layout_below="@id/layout_work1"
android:layout_width="match_parent"
android:layout_marginTop="10px"
android:orientation="horizontal"
android:paddingRight="6dp"
android:paddingLeft="6dp"
android:background="#60C5F1"
android:layout_height="150dp">

<ImageView
android:padding="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="160dp"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:background="#C8EFD4"
/>

<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:layout_height="160dp"
android:background="#C8EFD4"
/>

<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:layout_height="160dp"
android:background="#C8EFD4"
/>

</LinearLayout>

</RelativeLayout>

</LinearLayout>

总结:

这一篇主要讲了布局,都是在layout文件夹的.xml文件中实现,现在还没涉及到java文件。