美文网首页Android开发
《第二行代码》 第一章RelativeLayout

《第二行代码》 第一章RelativeLayout

作者: 你的益达233 | 来源:发表于2021-09-13 10:49 被阅读0次

它的子控件的属性是有规律可循的

一、居中属性

    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"  

其中一个示例代码:

<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=".groupview.RelativeLayoutActivity">

<TextView
    android:id="@+id/btn_system_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="TextView1"
    android:background="@drawable/btn_bg"
    android:textColor="@color/white"
    android:layout_marginTop="40dp"
    />
</RelativeLayout>  

二、相对父控件RelativeLayout对齐

    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"  

示例代码:

<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=".groupview.RelativeLayoutActivity">

<TextView
    android:id="@+id/btn_system_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:text="TextView1"
    android:background="@drawable/btn_bg"
    android:textColor="@color/white"
    android:textSize="32sp"
    />
</RelativeLayout>

效果图:

rl1.png

三、相对兄弟控件的对齐和定位

先说定位:
如:android:layout_toRightOf:我在谁的右边

示例代码:

<?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=".groupview.RelativeLayoutActivity">

<TextView
    android:id="@+id/tv_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView1"
    android:background="@drawable/btn_bg"
    android:textColor="@color/white"
    android:textSize="32sp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/tv_1"
    android:text="TextView2"
    android:background="@drawable/btn_bg"
    android:textColor="@color/white"
    android:textSize="32sp"/>
</RelativeLayout>  

效果图:

rl2.png

对齐:

 android:layout_alignLeft="@id/tv_1" //和tv_1左对齐
    android:layout_below="@id/tv_1" //在tv_1下面  

示例代码:

<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=".groupview.RelativeLayoutActivity">

<TextView
    android:id="@+id/tv_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView1"
    android:background="@drawable/btn_bg"
    android:textColor="@color/white"
    android:textSize="32sp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/tv_1"
    android:layout_below="@id/tv_1"
    android:text="TextView2"
    android:background="@drawable/btn_bg"
    android:textColor="@color/white"
    android:textSize="32sp"/>
</RelativeLayout>

效果图:

rl3.png

基本弄懂一个其他的就懂了

相关文章

网友评论

    本文标题:《第二行代码》 第一章RelativeLayout

    本文链接:https://www.haomeiwen.com/subject/kszxgltx.html