Transparent View over ImageView

2019-04-08 23:41发布

问题:

I'm blocked there. I'm trying to put a transparent View over a background. I've tried several methods.

throught XML with:

android:background="@color/transparent"

or

android:color="#80000000"  

or putting a reference to color.xml file as so

<resources>
    <color name="transp">#80000000</color>
</resources>

with my layout.xml like this

android:background="@color/transp"

I've also tried to do it by generated code

myView.getBackground().setAlpha(45);

or

myViewm.setBackgroundResource(R.color.trans);

I've seen some posts related, but none of the answers worked.

Besides which is even stranger is that all of these solutions seems to wrok fine on the GraphicalLayout in Eclipse. But when I launch my device, the screen remains not transparent.I've drawn a line on that view to make sure that something happens; and the line does show.

here is my layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

      <ImageView
        android:id="@+id/backgroundview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/space_bg"
        android:contentDescription="@string/desc" />

      <View 
        android:id="@+id/tileview"        
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/><!-- 
        android:background="@color/transp"/>-->

</RelativeLayout>

and my code

private ImageView bg;
    MyView tV;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        tV = new MyView(this);

setContentView(tV);

}

and the myView onDraw

@Override
    protected void onDraw(Canvas canvas)    
    {

        super.onDraw(canvas);
        this.setBackgroundResource(R.color.transp);
        canvas.drawLine(10,20,30,40, paint);

    }

So, where am I wrong? Thanks!!!

回答1:

android:background="@color/transparent"

You can use the transparent color provided in android resources:

android:background="@android:color/transparent"

android:color="#80000000"  

<resources>
    <color name="transp">#80000000</color>
</resources>

myViewm.setBackgroundResource(R.color.trans);

This will give you a very dark shade of gray. Alpha value of 80 is translucent at best.


myView.getBackground().setAlpha(45);

You may not be using this correctly.


private ImageView bg;
MyView tV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

    tV = new MyView(this);

    setContentView(tV);
}

This basically replaces the View inflated from R.layout.activity_main(which contains the ImageView and other widgets) with MyView. I don't think this is quite what you want. Try the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/backgroundview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/space_bg"
        android:contentDescription="@string/desc" />

    <RelativeLayout 
        android:id="@+id/tileview"        
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/some_drawable_smaller_than_screen_size"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</RelativeLayout>

How to inflate this xml:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
}

id="@+id/tileview" will be transparent and over id="@+id/backgroundview".



回答2:

android:alpha="0.5"

0.0 is totally transparent, 0.5 is medium transparent 1.0 is fully opaque transparent. **Here TextView is Transparent.

.

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

    <ImageView
        android:id="@+id/imageview_id"
        android:layout_width="match_parent"[![enter image description here][2]][2]
        android:layout_height="130dp"
        android:scaleType="centerCrop"
        android:src="@drawable/img3" />

    <TextView
        android:id="@+id/home_tvshow_textView_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="0.5"
        android:background="@color/red"
        android:gravity="center"
        android:text="Your String"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_17"
        android:layout_marginTop="@dimen/dp_110"/>


</RelativeLayout>