安卓,类QQ评论,界面编写

本文介绍了Android中的RelativeLayout布局原理,包括控件的相对位置、兄弟控件的定位方式、layout_marginStart与layout_marginLeft的区别,并通过实例代码展示了如何实现常见的布局效果。遇到的问题和解决方案也进行了分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.原理及部分知识点

1.1父布局的相对位置

layout_alignParentLeft 排列在父布局的左边 layout_alignParentRight 排列在父布局的右边
layout_alignParentBottom 排列在父布局的底部 layout_alignParentTop排列在父布局的顶部
layout_centerHorizontal 水平方向的正中间 layout_centerVertical 垂直方向的正中间
layout_centerInParent 整个父布局的正中心

1.2 兄弟控件

layout_toLeftOf 将该控件的右部置于给定ID的控件左边 layout_toRightOf 将该控件的左部置于给定ID的控件右边
layout_below 将该控件的底部置于给定ID的控件之下 layout_above 将该控件的底部置于给定ID的控件之上
layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐; layout_alignBottom
将该控件的顶部边缘与给定ID的顶部边缘对齐; layout_alignRight 将该控件的右部边缘与给定ID的右部边缘对齐;
layout_alignLeft 将该控件的左部边缘与给定ID的左部边缘对齐;

1.3 layout_marginStart与layout_marginLeft的区别

layout_marginStart与layout_marginLeft的区别有两种阅读方式,从左到右(left-to-right,即LTR)和从右到左(right-to-left,即RTL)。
简单来说,对于LTR,start、end等同于left、right;而对于RTL,则相反。
为了使用RTL布局,需要实现以下两点:

  1. 在AndroidManifest中声明支持RTL布局:在
    元素下添加android:supportsRtl="true"声明。
  2. 在App中用start、end来替代left、right:
  • 如果用4.2及以上编译(
    targetSdkVersion或者minSdkVersion大于等于17),则start、end来替代left、right,例如:android:paddingLeft
    应改为android:paddingStart

  • 如果用4.2以下编译(
    targetSdkVersion或者minSdkVersion小于等于16),两者都必须使用,例如:需要同时使用android:paddingLeft
    和android:paddingStart

Android4.2也引入了一些新的API来控制LTR和RTL模式,如:

  • android:layoutDirection
  • android:textDirection
  • android:textAlignment

2.实现效果

在这里插入图片描述在这里插入图片描述

3. 实现代码来源时出现的部分问题

1.原文使用了android:layout_alignParentLeft等控件,在写时出现功能有时无法实现的情况,转换为android:layout_alignParentLeft功能实现,原理不明。

4. 代码

<?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="200dp"
    tools:context=".MainActivity">

    <!--浏览次数,位于左下角-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="浏览5次"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"/>

    <!--
        人像
        将图片放大为50*50
        距离左边
    -->
    <ImageView
        android:id="@+id/img1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/ic_baseline_person_24" />

    <!--转发,需要id用来给下一个定位-->
    <ImageView
        android:id="@+id/share"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:background="@drawable/ic_baseline_share_24" />

    <!--评论,需要id给点赞定位-->
    <ImageView
        android:id="@+id/comment"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/ic_baseline_sms_24"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@id/share"
        android:layout_marginBottom="10dp"/>
    <!--点赞-->
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/ic_baseline_thumb_up_24"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@id/comment"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"/>
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="weixin_45856170"
        android:layout_toEndOf="@+id/img1"
        android:layout_marginTop="25dp"
        android:textColor="#000000"
        android:textSize="24sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="今天 16:43"
        android:layout_below="@+id/text1"
        android:layout_toEndOf="@id/img1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="测试用例1"
        android:textColor="#000000"
        android:textSize="24sp"
        />
</RelativeLayout>

5.参考文献

[1] 源代码来源:Android 入门第二讲02-相对布局RelativeLayout(线性布局缺点,相对布局属性,qq说说ui模仿,相对布局缺点)
[2] layout_marginStart与layout_marginLeft的区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值