2013-08-19 95 views
5

我在那裏被擋住了。我試圖在背景上放置透明視圖。 我試過幾種方法。ImageView的透明視圖

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

    } 

所以,我在哪裏錯了? 謝謝!

回答

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

這基本上替換從R.layout.activity_main充氣View(其中包含的ImageView和其他窗口小部件)與MyView。我不認爲這是你想要的。請嘗試以下操作:

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

如何誇大此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"將是透明的和超過id="@+id/backgroundview"

+2

男人...你是一個慷慨的真正的!!!! ...這個f *** g的作品!我的錯誤是我想把視圖放在imageView上?爲什麼它不工作?現在,如果我想在這個觀點上畫一些線,我可以通過代碼來實現嗎? – Antoine

+0

@Antoine很高興成功。 「現在,如果我想在這個觀點上畫一些線,我可以通過代碼來實現嗎?」「你必須比這更具體。你想繪製一些在ImageView上預定義的行(意思是:它們在活動開始時顯示)?或者,當你的應用運行時,你是否想繪製自由式(使用觸摸事件)? – Vikram

+1

對不起,因爲含糊不清...我想在活動開始時畫出一些線條,實際上用一些事件監聽器進行拼貼。並解釋爲什麼我自己的解決方案不起作用? – Antoine