0
点赞
收藏
分享

微信扫一扫

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

路西法阁下 2022-02-27 阅读 33

文章目录

1.原理及部分知识点

1.1父布局的相对位置

1.2 兄弟控件

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的区别

举报

相关推荐

0 条评论