我对齐两个 TextView,但当第一个 TextView 在 Android 应用程序中具有长行时,第二个 TextView 无法正确显示

发布于 2025-01-15 02:56:12 字数 1345 浏览 1 评论 0原文

我想对齐两个 TextView 并将第二个 TextView 放置在 Android 应用程序屏幕的右侧。 但第二个与外部屏幕被切断。 如果我在第一个 TextView 上写一条短信,第二个 TextView 会正确显示。

这是我的xml代码

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

    <TextView
        android:id="@+id/Txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:textSize="17dp"
        android:text="Hi, this line is long and affects right side " />

    <TextView
        android:id="@+id/Txt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/Txt1"
        android:gravity="right"
        android:maxLines="1"
        android:padding="12dp"
        android:text="Hello world"
        android:textSize="15dp" />

</RelativeLayout>

显示此内容

I want to align two TextViews and place the second one in the right side of the screen in Android app.
But the second one gets cut off from outside screen.
If I write a short message on the first TextView, the second one shows up correctly.

Here is my xml code

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

    <TextView
        android:id="@+id/Txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:textSize="17dp"
        android:text="Hi, this line is long and affects right side " />

    <TextView
        android:id="@+id/Txt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/Txt1"
        android:gravity="right"
        android:maxLines="1"
        android:padding="12dp"
        android:text="Hello world"
        android:textSize="15dp" />

</RelativeLayout>

This is displayed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2025-01-22 02:56:12
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity"
    >

    <TextView
        android:layout_weight="0.75"
        android:id="@+id/Txt1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:textSize="17dp"
        android:text="Hi, this line is long and affects right side " />

    <TextView
        android:layout_weight="0.25"
        android:id="@+id/Txt2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/Txt1"
        android:gravity="right"
        android:maxLines="1"
        android:padding="12dp"
        android:text="Hello world"
        android:textSize="15dp" />

</LinearLayout>

像这样的东西可以工作。在线性布局中使用布局权重。

<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity"
    >

    <TextView
        android:layout_weight="0.75"
        android:id="@+id/Txt1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:textSize="17dp"
        android:text="Hi, this line is long and affects right side " />

    <TextView
        android:layout_weight="0.25"
        android:id="@+id/Txt2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/Txt1"
        android:gravity="right"
        android:maxLines="1"
        android:padding="12dp"
        android:text="Hello world"
        android:textSize="15dp" />

</LinearLayout>

Something like this could work. Using layout weight in linear layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文