Android 如何不使用长文本视图将ImageView推离屏幕?
我希望imageview直接位于textview的右侧。我可以通过在TextView的属性中设置toLeftOf来实现这一点。但是对于非常长的文本字符串,我设置了省略号,当它达到布局的宽度后,它也会将ImageView推离 我可以通过将TextView的重力设置为1来显示图像视图,但随后图像视图将定位到右侧。我该怎么办Android 如何不使用长文本视图将ImageView推离屏幕?,android,android-layout,Android,Android Layout,我希望imageview直接位于textview的右侧。我可以通过在TextView的属性中设置toLeftOf来实现这一点。但是对于非常长的文本字符串,我设置了省略号,当它达到布局的宽度后,它也会将ImageView推离 我可以通过将TextView的重力设置为1来显示图像视图,但随后图像视图将定位到右侧。我该怎么办 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="@+id/title_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="1"
android:text="MY TEXT TO THE LEFT"
android:layout_toLeftOf="@id/img_button" />
<ImageButton
android:id="@+id/img_button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="6dp"
android:background="@drawable/some_img"
app:srcCompat="@drawable/ic_edit" />
</LinearLayout>
因此,设置权重=1,将图像拥抱到最右侧,我需要它是动态的,并且始终位于textview的右侧,然后当textview的宽度超过父视图时,图像就不会被推离屏幕。使用
线性布局
和方向=“水平”
属性。然后为LinearLayout设置weight\u sum=100
,然后为TextView和ImageView设置weight属性,只需根据需要放置weight属性即可。希望有帮助。使用带有orientation=“horizontal”
属性的LinearLayout
。然后为LinearLayout设置weight\u sum=100
,然后为TextView和ImageView设置weight属性,只需根据需要放置weight属性即可。希望有帮助。您可以使用并将文本视图宽度设置为0dp
(也称为匹配约束
)
如果文本太多,结果将导致textView删除一行
以下是一个例子:
<?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">
<ImageView
android:id="@+id/textView8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="something"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="@+id/textView10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/textView10" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="this is really long text and it wont push the green image because of android:layout_width attribute"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以使用并将文本视图宽度设置为0dp
(也称为匹配约束
)
如果文本太多,结果将导致textView删除一行
以下是一个例子:
<?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">
<ImageView
android:id="@+id/textView8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="something"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="@+id/textView10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/textView10" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="this is really long text and it wont push the green image because of android:layout_width attribute"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我将paddingRight设置为文本视图中图像按钮的大小,并在图像按钮上使用了alignRight。
并将容器更改为RelativeLayout。它按预期工作
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="@+id/title_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:paddingRight="22dp"
android:text="MY TEXT TO THE LEFT"
android:layout_toLeftOf="@id/img_button" />
<ImageButton
android:id="@+id/img_button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="6dp"
android:layout_alignRight="@+id/title_view"
android:background="@drawable/some_img"
app:srcCompat="@drawable/ic_edit" />
</RelativeLayout>
我将paddingRight设置为文本视图中图像按钮的大小,并在图像按钮上使用了alignRight。
并将容器更改为RelativeLayout。它按预期工作
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="@+id/title_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:paddingRight="22dp"
android:text="MY TEXT TO THE LEFT"
android:layout_toLeftOf="@id/img_button" />
<ImageButton
android:id="@+id/img_button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="6dp"
android:layout_alignRight="@+id/title_view"
android:background="@drawable/some_img"
app:srcCompat="@drawable/ic_edit" />
</RelativeLayout>
能否添加XML布局文件,以便更好地了解布局面临的问题?另外,请包括你想要的和你拥有的截图。这将有助于其他人更好地回答您的问题。我添加了一些xml@mdnaseemashra。为了更好,我可以将我的图像添加到textview的右上角吗?覆盖了吗?您可以添加XML布局文件以更好地了解布局面临的问题吗?另外,请包括你想要的和你拥有的截图。这将有助于其他人更好地回答您的问题。我添加了一些xml@mdnaseemashra。为了更好,我可以将我的图像添加到textview的右上角吗?覆盖了吗?它仍然将ImageView关闭。设置ImageView和textview的权重它仍然将ImageView关闭。设置ImageView和textview的权重