如何使textview的图像背景半透明而不是文本?

查图兰加

我需要动态设置文本视图的图像背景并使其半透明,文本完全不透明。

像这样的东西,但与 <21 API 兼容。

android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"

当我使用

textView.setBackgroundResource(x.getPicture());
textview.getBackground().setAlpha(80);

它可以工作,但后来当我将相同的资源用于图像视图时,即使我使用它也保持透明

imageView.setImageAlpha(255);

或者

imageView.setAlpha((float) 1.0);

这会影响整个文本视图,包括文本本身。

textView.setAlpha((float) 0.5);

我尝试了我能找到的一切,但没有找到解决方案。我究竟做错了什么?

整体布局xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="@drawable/bg1">

<include
    android:id="@+id/top_toolbar_details"
    layout="@layout/toolbar"
    android:layout_width="0dp"
    android:layout_height="68dp"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp" />

<LinearLayout
    android:id="@+id/ll_graphics"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details">

    <!--This is the imageview-->
    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:keepScreenOn="true"
        android:longClickable="true"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details" />

    <VideoView
        android:id="@+id/vv_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/iv_image"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <ScrollView
        android:id="@+id/sv_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">

        <!--This is the textview-->
        <TextView
            android:id="@+id/tv_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="20"
            android:paddingBottom="20dp"
            android:paddingEnd="20dp"
            android:paddingStart="20dp"
            android:paddingTop="10dp"
            android:scrollbars="vertical"
            android:textColor="@color/black"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@+id/iv_image"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details" />
    </ScrollView>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="5dp"
    android:orientation="horizontal"
    android:weightSum="1"
    app:layout_constraintBottom_toTopOf="@+id/llImg"
    app:layout_constraintHorizontal_bias="0.505"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">

    <ImageButton
        android:id="@+id/b_last_viewed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_shape_drawable"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_previous"
        app:layout_constraintBottom_toTopOf="@+id/llImg"
        app:layout_constraintHorizontal_bias="0.773"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/b_to_all"
        app:layout_constraintTop_toBottomOf="@+id/vv_video"
        app:layout_constraintVertical_bias="0.97" />

    <ImageButton
        android:id="@+id/b_to_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_shape_drawable"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_grid"
        app:layout_constraintBottom_toTopOf="@+id/llImg"
        app:layout_constraintTop_toBottomOf="@+id/vv_video"
        app:layout_constraintVertical_bias="0.95"
        tools:layout_editor_absoluteX="176dp" />

    <ImageButton
        android:id="@+id/b_to_favourites"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_shape_drawable"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_favourite"
        app:layout_constraintBottom_toTopOf="@+id/llImg"
        app:layout_constraintHorizontal_bias="0.164"
        app:layout_constraintLeft_toRightOf="@+id/b_to_all"
        app:layout_constraintRight_toRightOf="parent" />
</LinearLayout>

<LinearLayout
    android:id="@+id/llImg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:layout_editor_absoluteX="0dp">

    <android.support.v17.leanback.widget.HorizontalGridView
        android:id="@+id/gv_small_preview"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b_last_viewed"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="0dp" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>
零一

只需重新调整布局

//this is your root
<RelativeLayout>
     //this is your background
     //add your semi transparent for below imageView
     <ImageView android:height="match_parent" android:witdh="match_parent"/>

     //this is your text
     <TextView/ >

</RelativeLayout>

然后以编程方式

imageView.setImageAlpha(255); //it will not affect the textview

或者只是android:alpha在 imageView 中添加xml

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CSS:半透明背景和图像

来自分类Dev

如何在Pygame中动态制作半透明图像

来自分类Dev

如何在半透明背景上创建透明区域(div)?

来自分类Dev

如何使UIPageViewController背景半透明?

来自分类Dev

如何使jPanel半透明?

来自分类Dev

如何使RelativeLayout半透明而不是活动

来自分类Dev

如何通过半透明背景停止点击事件?

来自分类Dev

半透明的倾斜背景

来自分类Dev

如何在ImageList中使用半透明图像

来自分类Dev

如何仅对背景图像而不是文本应用效果

来自分类Dev

如何使布局半透明?

来自分类Dev

如何使Terminal半透明?

来自分类Dev

如何使jpg图像半透明?

来自分类Dev

SwiftUI:fullScreenCover的半透明背景

来自分类Dev

如何在相机ARKit上添加半透明背景

来自分类Dev

如何使Terminal半透明?

来自分类Dev

如何在Pygame中动态制作半透明图像

来自分类Dev

带有透明背景图像的文本

来自分类Dev

如何使UIPageViewController背景半透明?

来自分类Dev

如何在文本字段中使用图像设置HTML表单的半透明背景

来自分类Dev

如何在按钮上的半透明图像上实现文本

来自分类Dev

ModalViewController半透明背景

来自分类Dev

如何在半透明图像周围绘制边框?(css)

来自分类Dev

如何删除片段中的半透明灰色背景?

来自分类Dev

如何在ImageList中使用半透明图像

来自分类Dev

如何仅对背景图像而不是文本应用效果

来自分类Dev

如何使用 ATL::CImage 旋转并使图像同时半透明?

来自分类Dev

背景透明但不是文本

来自分类Dev

内容和背景图像之间的半透明层 - CSS