- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
但我希望我的按钮仅位于文本的末尾,并且在文本较大时不会移出屏幕:
这是我的布局:
<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>
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>
修改 TextView
的 layout_constraintHorizontal_bias
属性以适应您想要的布局。
关于android - 如何将 ButtonView 定位到具有大单行文本的 TextView 的末尾而不将其移出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58144502/
我正在使用抽屉导航。我使用 android:theme="@android:style/Theme.Light.NoTitleBar" 隐藏了操作栏。所以我只能使用滑动来滑动抽屉。我想创建一个按钮。单
我试图在 Switch 关闭时禁用 EditText 我已经为它制作了 If-else。但是我的CompoundButton buttonView无法解析。可能是什么问题?这是我的代码: public
我在 TextView 的末尾有一个 Button: 当文本增加时,我得到以下信息: 但我希望我的按钮仅位于文本的末尾,并且在文本较大时不会移出屏幕: 这是我的布局:
我在 (0,0) 位置有一个按钮,当我点击它时,它应该移动到 (-x,-y) 位置,例如 (-0,-2) 位置或 (x,-y) 位置for ex (0,-2) position.How to impl
我是一名优秀的程序员,十分优秀!