2011-10-29 39 views
2

我已經找了幾個小時來回答這個看似簡單的問題。從圖層列表中繪製的圖像按鈕?

基本上,你如何做一個圖層按鈕列表中的某個drawable?

當Eclipse運行時,它不會將任何錯誤標記爲此代碼,所以我得到一個強制關閉。我錯過了什麼?

而且 -

難道我用findViewById當他們在一個層列表,甚至引用的資源呢?

XML文件中的android:id是否屬於item標記或bitmap標記?

編輯:此代碼只是我放在一起快速展示我的問題。我正在嘗試使最後一個分層繪製活動按鈕。例如,對於一個用戶界面,我將一堆或多個圖形放在另一個上面,最後兩個圖層將是我想要激活的按鈕。那可能嗎?或者,也許有更好的方法來做我想做的事情?

謝謝。


layers.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <bitmap android:src="@drawable/android_red" 
     android:gravity="center" /> 
    </item> 
    <item android:top="10dp" android:left="10dp"> 
     <bitmap android:src="@drawable/android_green" 
     android:gravity="center" /> 
    </item> 
    <item android:id="@+id/blue_button" 
     android:top="20dp" android:left="20dp"> 
     <bitmap android:src="@drawable/android_blue" 
     android:gravity="center" /> 
    </item> 
</layer-list> 

main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:src="@drawable/layers" /> 

</LinearLayout> 

活動:

public class LayoutTestActivity extends Activity implements OnClickListener { 

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

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FILL_PARENT,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.main); 

     ImageButton imgStartButton = (ImageButton) findViewById(R.id.blue_button); 
     imgStartButton.setOnClickListener(this); 


     } 

    public void onClick(View v) { 


    } 
} 

回答

1

您不會將id添加到可繪製的圖層列表,而是添加到Imageview。

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:src="@drawable/layers" 
    android:id="@+id/blue_button"/> 

要訪問繪製您使用

ImageView imgStartButton = (ImageView) findViewById(R.id.blue_button); 
imgStartButton.setOnClickListener(this); 
imgStartButton.setBackgroundResource(R.drawable.layers); 
+0

現在我明白了。非常感謝你。 – ShadowGod

+0

這是進步,但並不完全如我所願。如果我將onClickListener設置爲整個視圖,則可以單擊視圖上的任意位置以註冊點擊。我想限制它只是圖層中的一個可繪圖,這是否有意義?想象一下,如果我將一些圖形分層疊放在一起,最後一層是一個按鈕,那麼我只想將最後一層圖像製作爲活動按鈕。 – ShadowGod

+0

clicl監聽器被賦予不可繪製的視圖。根據您的要求,您需要使用將視圖疊放在一起的框架佈局。並將每個圖層添加爲每個視圖。將clocklistener添加到適當的視圖。 – blessenm