【问题标题】:how to add text with highlight color background it to ImageView如何将具有突出显示颜色背景的文本添加到 ImageView
【发布时间】:2019-08-04 16:21:51
【问题描述】:

我尝试将带有突出显示颜色的文本添加到 ImageView,我进行了一些搜索,我找到了这个 answer 并成功应用它,但我需要在图像中使用黑色这样的文本的背景突出显示

destination

当前的 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/postImage"
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:layout_margin="2dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/postTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/postImage"
        android:layout_alignEnd="@id/postImage"
        android:layout_alignRight="@id/postImage"
        android:layout_alignLeft="@id/postImage"
        android:layout_alignTop="@+id/postImage"
        android:layout_alignBottom="@+id/postImage"
        android:layout_margin="1dp"
        android:ellipsize="end"
        android:gravity="center|bottom"
        android:maxLines="2"
        android:text="Hello"
        android:textColor="@color/white"
        android:textSize="14sp" />
</RelativeLayout>

【问题讨论】:

  • 需要为Textview设置背景颜色
  • @LiemVo 已将颜色设置为白色
  • 我的意思是 android:background 而不是 textView 的 android:textColor。背景的值可以是颜色、可绘制、...

标签: android android-layout android-imageview textview android-xml


【解决方案1】:

简单的方法是:在文本视图中添加背景颜色,并根据透明度设置 alpha 值。

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="lasdfjsdfjlsdkfj"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
        android:layout_alignBottom="@id/image"
        android:layout_alignLeft="@id/image"
        android:textSize="18sp"
        android:layout_alignRight="@id/image"
        android:alpha="0.5"/>

</RelativeLayout>

【讨论】:

  • 调整 alpha 值以获得所需的透明度。介于 0 和 1 之间的值。
【解决方案2】:

您可以使用ConstraintLayout。你还需要创建drawable文件

你的布局应该是这样的;

<android.support.constraint.ConstraintLayout
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp">

    <ImageView
            android:id="@+id/postImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:src="@android:drawable/stat_sys_headset"/>


    <TextView
            android:id="@+id/postTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@drawable/border_bottom_rounded"
            android:paddingBottom="2dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="title"/>


</android.support.constraint.ConstraintLayout>

您的可绘制文件 (border_bottom_rounded) 应该是这样的;

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#A9000000"/>
    <stroke android:width="1dp" android:color="#A9000000" />
    <corners android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

【讨论】:

  • 应用后,我看到空白页!,ConstraintLayout 是否与 GridLayoutManager 正常工作?
  • 你有日志吗?我认为它可以正常与 GridLayoutManager 一起使用。
  • 不,我只是复制了整个布局,我会用当前的 RelativeLayout 尝试这种可绘制样式,也许可行
【解决方案3】:

您可以使用@Ergin Ersoy 和@Ravi 建议的两个选项

创建可绘制文件后(border_bottom_rounded)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#A9000000"/>
    <stroke android:width="1dp" android:color="#A9000000" />
    <corners android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

将其设置为TextView的背景,我也建议将android:alpha="1"设置为0.5以使文本清晰

完整的结果 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/postImage"
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:layout_margin="2dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/postTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/postImage"
        android:layout_alignLeft="@id/postImage"
        android:layout_alignEnd="@id/postImage"
        android:layout_alignRight="@id/postImage"
        android:layout_alignBottom="@+id/postImage"
        android:layout_margin="1dp"
        android:alpha="1"
        android:background="@drawable/border_bottom_rounded"
        android:ellipsize="end"
        android:gravity="center|bottom"
        android:maxLines="2"
        android:text="Hello"
        android:textColor="@color/white"
        android:textSize="14sp" />
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多