2009-06-07 76 views
70

Android中可能存在重疊視圖嗎?我想在前面有一個透明png的ImageView,後面是另一個視圖。Android中的重疊視圖

編輯:

這是我目前所面對的是什麼,問題是,在ImageView的圖像不是透明的,這應該是透明的部分只是黑色的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/gallerylayout" 
> 
<Gallery android:id="@+id/overview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

<ImageView android:id="@+id/navigmaske" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/navigmask" 
    /> 

</RelativeLayout> 

編輯:

我得到它的工作,它是從對球隊的另一個程序員主題文件。 就改變了這種

<item name="android:background">#FF000000</item> 

這個

<item name="android:background">#00000000</item> 
+0

你有什麼應該工作 - 你肯定圖庫有可見的內容,navigmaske drawable是一個透明的PNG? – 2009-06-07 17:09:06

+0

是的,畫廊是可見的沒有覆蓋,我敢肯定,PNG是透明的 – 2009-06-07 20:45:54

回答

68

Android在本地處理視圖和繪圖(包括PNG圖像)的透明度,因此您描述的場景(在Gallery前部分透明的ImageView)肯定是可能的。

如果您有問題,它可能與佈局或圖像有關。我複製了你描述的佈局,併成功實現了你之後的效果。這是我使用的確切佈局。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallerylayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Gallery 
    android:id="@+id/overview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
    <ImageView 
    android:id="@+id/navigmaske" 
    android:background="#0000"  
    android:src="@drawable/navigmask" 
    android:scaleType="fitXY" 
    android:layout_alignTop="@id/overview" 
    android:layout_alignBottom="@id/overview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</RelativeLayout> 

請注意,我已經改變了父RelativeLayout到的高度和寬度fill_parent作爲一般是你想要的一個主要活動。然後,我已將ImageView的頂部和底部對齊到Gallery的頂部和底部,以確保它位於其前面。

我也明確地設置了ImageView的背景是透明的。

至於圖像可繪製本身,如果你把PNG文件放在某處讓我看,我可以在我的項目中使用它,看看它是否是負責任的。

+0

不錯!我試圖設置一個ImageView背景#0000(和@android:color/transparent)...無濟於事。如果我嘗試淡化它(alpha從1.0降低到0.0),它會淡入黑色,而不是淡入透明。這是在Android 2.0上。 – 2009-12-15 03:19:56

-2

是的,這是可能的。然而,挑戰是正確地進行佈局。最簡單的方法是使用AbsoluteLayout,然後將這兩個圖像放在您想要的位置。除了將它稍後添加到佈局之外,您不需要爲透明png做任何特殊的操作。

+15

AbsoluteLayout現在是折舊。沒有人應該再使用它了。 – num1 2012-01-15 21:53:03

1

可見畫廊改變了可見性,這是​​你如何讓畫廊超過其他視圖重疊。 Home示例應用程序有這個技術的一些很好的例子。

9

此外,看看FrameLayout,這是相機的圖庫應用程序如何實現縮放按鈕覆蓋。

+1

我一直都在爲`FrameLayout`去尋找完美的目的。然而,我現在才發現,在API 11之前,`FrameLayout`無法將最大測量高度傳播給它的所有孩子,[見這裏](http://stackoverflow.com/q/15780884/1856738)。因此,爲了支持Android 2,除非有預定的高度,否則必須使用`RelativeLayout`。 – 2013-04-04 06:16:27

0

的方式角落找尋simples是把-40dp緣上方的ImageView的BUTTOM