首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android的左、右剪裁差异

Android的左、右剪裁差异
EN

Stack Overflow用户
提问于 2019-02-17 10:05:38
回答 1查看 24关注 0票数 0

我看到了左对齐和右对齐的TextView在裁剪上的不同之处,这是我不明白的。

下面是一个样例布局:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <RelativeLayout
        android:id="@+id/fullLay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left">

        <RelativeLayout
            android:id="@+id/layout_message_content_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:background="@color/blue">

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Hello World"/>
        </RelativeLayout>

        <TextView
            android:id="@+id/timestamp_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layout_message_content_container"
            android:maxLines="1"
            android:layout_alignLeft="@+id/layout_message_content_container"
            android:text="This is a long sample label string"/>
    </RelativeLayout>

</LinearLayout>

这给出了我想要的布局:

标题标签在“Hello World”的下方。但是,如果我将其更改为android:gravity=“正确”,并使用layout_alignRight而不是layout_alignLeft,标题视图将被裁剪。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <RelativeLayout
        android:id="@+id/fullLay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <RelativeLayout
            android:id="@+id/layout_message_content_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:background="@color/blue">

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Hello World"/>
        </RelativeLayout>

        <TextView
            android:id="@+id/timestamp_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layout_message_content_container"
            android:maxLines="1"
            android:layout_alignRight="@+id/layout_message_content_container"
            android:text="This is a long sample label string"/>
    </RelativeLayout>

</LinearLayout>

现在标题视图被裁剪到它上面的视图的宽度。

如何使此视图与其上方的视图右对齐,但不裁剪?

EN

Stack Overflow用户

回答已采纳

发布于 2019-02-17 11:13:09

试试这个-

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/layout_message_content_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:background="@color/blue"
        android:padding="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World" />
    </RelativeLayout>

    <TextView
        android:id="@+id/timestamp_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:maxLines="1"
        android:text="This is a long sample label string" />

</LinearLayout>

ConstraintLayout的用户界面相同

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        android:padding="15dp"
        android:text="Hello World"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/timestamp_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="This is a long sample label string"
        android:textAlignment="textEnd"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/hello_world" />

</android.support.constraint.ConstraintLayout>
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54729490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档