您的位置:首页 > 其它

LinearLayout和RelativeLayout的使用

2016-08-23 19:36 246 查看

使用LinearLayout实现下图的界面



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

<!-- 第一层   1 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff0000"
android:orientation="vertical" >

<!-- 第二层   1  horizontal -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ff66" />

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#ff0066" />

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0066ff" />
</LinearLayout>

<!-- 第二层   1  horizontal -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff8866" />

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#33ff66" />
</LinearLayout>
</LinearLayout>

<!-- 第一层   2 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#0000ff"
android:orientation="horizontal" >
<!-- 垂直的线性布局分成两部分 -->
<!-- 第一部分 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#0000ff"
android:orientation="vertical" >
<!-- 里面有两行 -->
<!-- 第一行 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#448811" />

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#ff9900" />

</LinearLayout>
<!-- 第二行 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ffff"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<!-- 第二部分 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000ff"
android:orientation="vertical" >
</LinearLayout>

</LinearLayout>

<!-- 第一层   1 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ff00"
android:orientation="vertical" >

<!-- 第一层的第一层   1 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#33ff00"
android:orientation="vertical" >

<!-- 第一层的第一层(1)   1 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

b066
android:background="#ffff00"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff8866" />

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#33ff66" />
</LinearLayout>
</LinearLayout>
<!-- 第一层的第二层   1 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffff00"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

</LinearLayout>

Layout实现下图的界面



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 50dp的TextView -->
<TextView
android:id="@+id/tv1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#66ff66"
android:text=" "
/>
<TextView
android:id="@+id/tv2"
android:layout_below="@id/tv1"
android:layout_toRightOf="@id/tv1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#66ff66"
android:text=" "
/>
<TextView
android:id="@+id/tv3"
android:layout_below="@id/tv2"
android:layout_toRightOf="@id/tv2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#66ff66"
android:text=" "
/>
<TextView
android:id="@+id/tv4"
android:layout_below="@id/tv3"
android:layout_toLeftOf="@id/tv3"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#66ff66"
android:text=" "
/>
<TextView
android:id="@+id/tv5"
android:layout_below="@id/tv4"
android:layout_toLeftOf="@id/tv4"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#66ff66"
android:text=" "
/>
<TextView
android:id="@+id/tv6"
android:layout_below="@id/tv4"
android:layout_toRightOf="@id/tv4"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#66ff66"
android:text=" "
/>
<!-- 20dp的TextView -->
<TextView
android:layout_alignTop="@id/tv2"
android:layout_toRightOf="@id/tv2"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#9900ff"
android:text=" "
/>
<TextView
android:layout_alignBottom="@id/tv2"
android:layout_toLeftOf="@id/tv2"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#9900ff"
android:text=" "
/>
<TextView
android:layout_alignBottom="@id/tv3"
android:layout_toRightOf="@id/tv3"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#9900ff"
android:text=" "
/>
<TextView
android:layout_alignLeft="@id/tv5"
android:layout_above="@id/tv5"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#9900ff"
android:text=" "
/>
<TextView
android:layout_alignRight="@id/tv6"
android:layout_below="@id/tv6"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#9900ff"
android:text=" "
/>
</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息