ImageView上的透明视图

时间:2013-08-19 18:51:44

标签: android view transparent

我被封锁了。我正试图在背景上放置透明视图。 我尝试了几种方法。

通过XML:

android:background="@color/transparent"

android:color="#80000000"  

或将color.xml文件的引用视为

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

使用我的layout.xml

android:background="@color/transp"

我也试图通过生成的代码

来做到这一点
myView.getBackground().setAlpha(45);

myViewm.setBackgroundResource(R.color.trans);

我看过一些相关的帖子,但没有一个答案有效。

除此之外,更奇怪的是所有这些解决方案似乎都在Eclipse的GraphicalLayout上得到了很好的解决。 但是当我启动我的设备时,屏幕仍然不透明。我在该视图上画了一条线以确保发生了某些事情;并且线确实显示。

这是我的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>

和我的代码

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);

}

和myView onDraw

@Override
    protected void onDraw(Canvas canvas)    
    {

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

    }

那么,我哪里错了? 感谢!!!

4 个答案:

答案 0 :(得分:11)

android:background="@color/transparent"

您可以使用android资源中提供的transparent颜色:

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

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

myViewm.setBackgroundResource(R.color.trans);

这会给你一个非常黑暗的灰色阴影。 80的Alpha值最多是半透明的。

  
myView.getBackground().setAlpha(45);

您可能没有正确使用此功能。

  
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);
}

这基本上取代了View R.layout.activity_mainMyView(其中包含ImageView和其他小部件)<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> @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"

如何给这个xml充气:

id="@+id/backgroundview"

{{1}}将透明且超过{{1}}。

答案 1 :(得分:2)

机器人:阿尔法= “0.5”

0.0是完全透明的,0.5是中等透明的1.0是完全不透明的透明。 **这里TextView是透明的。

enter image description hereenter image description here

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

答案 2 :(得分:0)

  • 实现手动查看

如果您将相对布局用作布局中的父标记。

您的布局应该是这样的。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
.
.
.
    <View
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#EBE9E9"
       android:id="@+id/hide_view"/>
</RelativeLayout>

活动中

hide_view = findViewById(R.id.hide_view);
hide_view.setAlpha(0.5f);

答案 3 :(得分:0)

根据您的小部件设置

android:color =“#80000000”