2011-10-29 36 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