android - 如何使用大单行文本将 ButtonView 定位到 TextView 的末尾,而不是将其移出屏幕
问题描述
但是我希望我的按钮仅位于文本的末尾,并且在文本很大时不会移出屏幕:
这是我的布局:
<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_weight从FrameLayout移动到AppCompatTextView那么我会得到需要的结果,但是使用小文本我会得到以下信息:
我尝试使用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>
结果
附加 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>
修改layout_constraintHorizontal_bias
的属性TextView
以适合您想要的布局。
推荐阅读
- scala - Selenium on Docker:在同一个 docker-compose 文件中测试 Docker 镜像
- python - “unicode = unicode”是什么意思?
- c# - CORS issues after upgrading to .NET Core 2.2
- jquery - how to set a value to an input field onclick with jquery
- css - 使用两种背景的最佳实践
- angular - How to load multiple rest requests
- python - Python Flask:返回文本中的链接不起作用
- r - 将误差线与条形图因子水平对齐
- outlook-addin - 如何避免为线程中的所有消息添加标记类别?
- regex - 正则表达式删除论坛上的空格