gpt4 book ai didi

android - 如何将 ButtonView 定位到具有大单行文本的 TextView 的末尾而不将其移出屏幕

转载 作者:行者123 更新时间:2023-11-29 22:48:35 25 4
gpt4 key购买 nike

我在 TextView 的末尾有一个 Button:
small text

当文本增加时,我得到以下信息:
button behind the screen

但我希望我的按钮仅位于文本的末尾,并且在文本较大时不会移出屏幕:
needed result

这是我的布局:

<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:padding="10dp"
android:gravity="center_vertical"
android:orientation="horizontal" >

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Long text without button"
android:textColor="#000"
android:textSize="36sp" />

<FrameLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" >

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="Button" />

</FrameLayout>
</LinearLayout>

如果我将 layout_weightFrameLayout 移动到 AppCompatTextView 然后我得到需要的结果,但是对于小文本我得到以下内容:
bad result

我尝试了 LinearLayout 权重和 ConstraintLayout,但没有任何帮助。
如何实现?

最佳答案

您只需使用 ConstraintLayout

试试这个

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text"
android:layout_width="0dp" <-- It means using match-constraint.
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Long text without button"
android:textColor="#000"
android:textSize="36sp"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@id/text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text"
app:layout_constraintTop_toTopOf="@id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果

enter image description here

附加 XML - 版本 wrap_content

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="short text"
android:textColor="#000"
android:textSize="36sp"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@id/text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text"
app:layout_constraintTop_toTopOf="@id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

修改 TextViewlayout_constraintHorizo​​ntal_bias 属性以适应您想要的布局。

关于android - 如何将 ButtonView 定位到具有大单行文本的 TextView 的末尾而不将其移出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58144502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com