这一章主要讲解安卓两大常用布局,RelativeLayout和LinearLayout
RelativeLayout指的是相对布局,控件在该布局中都是相对位置,LinearLayout指的是线性布局,有水平线性和垂直线性。
下面通过一张设计图来解释怎么使用者两个布局。
两个黑框中的内容使用了横向的LinearLayout
红色框中的内容使用了相对布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="match_parent"
tools:context=".Main2Activity">
<LinearLayout
android:id="@+id/first_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="2"
android:background="@color/colorPrimaryDark"/>
<EditText
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_weight="6"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="2"
android:background="@color/colorPrimaryDark"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/second_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/first_line"
android:layout_marginTop="50dp"
android:weightSum="5">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="综合"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="销量"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="视频"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="图标"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="筛选"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_below="@+id/second_line">
<ImageView
android:id="@+id/thumnail"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@color/colorAccent"/>
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="手机智能手机 月销过万 vivoZ5X骁龙处理器4G"
android:layout_toRightOf="@+id/thumnail"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:maxLines="2"
android:ellipsize="end"
android:textSize="20sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6.35英寸 | 超长待机 | 上市:2019-5"
android:layout_below="@+id/desc"
android:layout_toRightOf="@+id/thumnail"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:textSize="13sp"/>
</RelativeLayout>
</RelativeLayout>