JIANGzihao0222

导航

 

 

一:纵向布局
<?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">//纵向布局
<ImageView
android:background="@color/black"
android:layout_width="200dp"
android:layout_height="200dp"/>
<ImageView
android:background="#FF0000"
android:layout_width="200dp"
android:layout_height="200dp"/>
<ImageView
android:background="#00FFFF"
android:layout_width="200dp"
android:layout_height="200dp"
</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="horizontal">//横向布局(默认)
<ImageView
android:background="@color/black"
android:layout_width="100dp"
android:layout_height="200dp"/>
<ImageView
android:background="#FF0000"
android:layout_width="100dp"
android:layout_height="200dp"/>
<ImageView
android:background="#00FFFF"
android:layout_width="100dp"
android:layout_height="200dp"/>
</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:gravity="center_horizontal|bottom"//对齐方式,多个组合
android:orientation="horizontal">
<ImageView
android:background="@color/black"
android:layout_width="100dp"
android:layout_height="200dp"/>
<ImageView
android:background="#FF0000"
android:layout_width="100dp"
android:layout_height="200dp"/>
<ImageView
android:background="#00FFFF"
android:layout_width="100dp"
android:layout_height="200dp"/>
</LinearLayout>
结果演示:

 


子类对齐:

未添加前:

添加: android:layout_gravity="center_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">
<ImageView
android:background="@color/black"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>
<ImageView
android:background="#FF0000"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>

<ImageView
android:background="#00FFFF"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>

</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:divider="@drawable/divider"//分割线
android:showDividers="middle"//分割位置
android:dividerPadding="100dp"
android:orientation="vertical">
<ImageView
android:background="@color/black"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>
<ImageView
android:background="#FF0000"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>

<ImageView
android:background="#00FFFF"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
结果:
第二种方法:绘制
<View
android:background="@color/purple_500"
android:layout_width="wrap_content"
android:layout_height="10dp"/>
结果:

五:权重(按比例分配剩余空间,无剩余则无法分配)

 

<?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:dividerPadding="100dp"
android:orientation="vertical">
<ImageView
android:background="@color/black"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_weight="1"//剩余空间全部给(因为一)
android:layout_gravity="center_horizontal"/>
<ImageView
android:background="#FF0000"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>

<ImageView
android:background="#00FFFF"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"/>

 

注意:match_parent的相关操作。

 

 



 

 

 


 




 

 

 

posted on 2023-02-26 16:11  实名吓我一跳  阅读(97)  评论(0)    收藏  举报