【问题标题】:Using wrap_content without content going of the screen and without using layout_weight使用 wrap_content 而不使用屏幕内容且不使用 layout_weight
【发布时间】:2020-09-15 20:01:55
【问题描述】:

所以我有这样的事情:

LinearLayout:
   LinearLayout - wrap_content, background black:
      TextView - wrap_content
   LinearLayout:
      Image - 20dp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center|start"
    android:padding="5dp"
    android:layout_margin="2dp">


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="klgjdkljgilfdjgidfhjiogjdio;jhgiju;fgjdhjgu;hdfuighdfuk;ghduk;fhgukldhfgujkdfhyufhglfdhguldfhguklhfdulghldfughyulfhdulghdfulghul"
        android:textColor="#D5D5D5" />

    <LinearLayout
        android:layout_width="15dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginBottom="2dp"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_gravity="bottom|center"
            android:src="@drawable/edit" />
    </LinearLayout>
</LinearLayout>

这很好用,但是当 TextView 包含大于屏幕大小的文本时,图像会超出屏幕。我不希望这种情况发生。

示例如下:

有谁知道如何解决这个问题,这样它就不会出现在屏幕之外?

谢谢!

【问题讨论】:

  • 如果你也上传你的 XML 更好理解
  • 我的 XML 文件太长,但可以添加。
  • 您只是为您提到的视图添加
  • 修复文本视图的宽度添加自定义宽度或使用约束布局
  • 我希望 TextView 的宽度是动态的,但也不要在最后截断图像。

标签: android xml android-studio layout


【解决方案1】:

ConstraintLayout试试这个

<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="klgjdkljgilfdjgidfhjiogjdio;jhgiju;fgjdhjgu;hdfuighdfuk;ghduk;fhgukldhfgujkdfhyufhglfdhguldfhguklhfdulghldfughyulfhdulghdfulghul"
        android:textColor="#D5D5D5"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@id/image"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


    <ImageView
        android:id="@+id/image"
        android:layout_width="20dp"
        android:layout_height="10dp"
        android:layout_gravity="bottom|center"
        android:src="@drawable/edit"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/message"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2014-10-27
    • 2017-04-02
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多