android - Android Gravity Left vs Right Clipping 差异
问题描述
我看到左对齐和右对齐 TextView 之间的剪辑存在差异,我不明白。
这是一个示例布局:
<?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="right" 并使用 layout_alignRight 而不是 layout_alignLeft,则标题视图会被剪裁。
<?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>
现在标题视图被剪裁到其上方视图的宽度。
我怎样才能让这个视图与它上面的视图右对齐但不被剪裁?
解决方案
尝试这个 -
<?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
<?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>
推荐阅读
- php - Laravel 5.6: ErrorException: compact(): Undefined variable: operator
- xml - 如何从 XmlEvent 访问 XML 元素名称
- flutter - 当该属性更改时,如何对显示对象属性的文本小部件进行反应性更新?这与 GetX
- elasticsearch - 如何在一个仪表板kibana中显示不同的时间段?
- linux - 为什么 ldconfig 能够找到一个库,而不是 Rust?
- c# - 并发 AWS Lambdas 运行不止一次
- npm - 将依赖项安装为 .tgz 的 NPM 包
- python - Numpy 数组 - 使用 reshape 将数组末尾的多列堆叠为行
- python - django:粘贴使用文本区域中的选择文件按钮收集的 pdf 文件的文本
- ios - 如何在 pdf 视图上移动/拖动多个视图?