2017-05-04 51 views
0

我需要动态设置一个textview的图像背景,并使其具有完全不透明度的半透明。如何使textview的图像背景半透明但不是文本?

类似这样做,但与< 21 API兼容。

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

当我使用

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

它的工作原理,但后来当我使用相同的资源,以一个imageview的它,即使我用保持透明

imageView.setImageAlpha(255); 

imageView.setAlpha((float) 1.0); 

而这影响整个te xtview包括文本本身。

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> 
+0

添加android:alpha首先尝试setAlpha上的图片,然后将其设置为背景 –

回答

0

只是重新调整布局

//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 

,或只是将XML格式的ImageView的

+0

这是一个好主意,我实际上在textview下有imageview,但是当切换它们时我改变了它们的可见性,因为我的布局非常复杂,并且我不能让它在尝试显示时弄乱整个布局imageview和textview在同一时间。你认为你可以看看我的代码,并试图告诉我如何在我的情况下做到这一点?我会将代码粘贴到原始帖子中。 – chaturanga

+0

我试图按照您的建议嵌套相对布局,但它使textview和视频视图高度0dp尽管一切都设置为match_parent。第一个线性布局大小是根据屏幕大小动态计算的,这就是为什么它被设置为0x0dp。它可以工作,但是当相对布局被添加时,它不再工作。 – chaturanga

+0

确定0dp高度显然是由可见性设置消失引起的,当设置为/可见时,它在蓝图中看起来很好,但是当某些操作改变可见性(点击或滑动)时,它仍然不会出现在应用中。我跑出了想法如何解决它。 – chaturanga

0

试试这个:

int myColor = Color.parseColor("#80FFFFFF"); 
Drawable backgroundColor = new ColorDrawable(myColor); 
backgroundColor.setAlpha(yourAlpha); 

textView.setBackground(backgroundColor); 
+0

谢谢,但我在后台需要的图像,并把它与半透明至半透明或它覆盖颜色,这只会使透明的颜色。 – chaturanga