如何使textview的图像背景半透明而不是文本?
How to make image background of textview semi transparent but not the text?
我需要动态设置文本视图的图像背景并使其半透明,文本完全不透明。
类似这样的东西,但与 <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>
试试这个:
int myColor = Color.parseColor("#80FFFFFF");
Drawable backgroundColor = new ColorDrawable(myColor);
backgroundColor.setAlpha(yourAlpha);
textView.setBackground(backgroundColor);
只需重新调整您的布局
//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
或者在 imageView
的 xml 中添加 android:alpha
我需要动态设置文本视图的图像背景并使其半透明,文本完全不透明。
类似这样的东西,但与 <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>
试试这个:
int myColor = Color.parseColor("#80FFFFFF");
Drawable backgroundColor = new ColorDrawable(myColor);
backgroundColor.setAlpha(yourAlpha);
textView.setBackground(backgroundColor);
只需重新调整您的布局
//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
或者在 imageView
的 xml 中添加android:alpha