android - 图层列表中可绘制的图像按钮?

标签 android drawable imagebutton layer

对于我遇到的这个看似简单的问题,我已经寻找了几个小时的答案。

基本上,您如何使图层列表中的某个可绘制对象成为图像按钮?

Eclipse 未将此代码标记为任何错误,但当它运行时我强制关闭。我错过了什么?

还有——

我是否使用 findViewById 来引用资源,即使它们在图层列表中?

XML文件中的android:id属于item标签还是bitmap标签?

编辑:这段代码只是我为了演示我的问题而快速组合起来的。我正在尝试使最后一个分层可绘制对象成为 Activity 按钮。例如,对于一个 UI,我将一堆或图形叠加在一起,最后两层将是我想要激活的按钮。那可能吗?或者也许有更好的方法来完成我想做的事情?

谢谢。


图层.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>

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

Activity :

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


    }
}

最佳答案

您不将 id 放入可绘制图层列表,而是放入 Imageview。

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

访问你使用的drawable

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

关于android - 图层列表中可绘制的图像按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937182/

相关文章:

android - 如何解决 setBackgroundDrawable 被弃用的问题

android - 如何以编程方式在 Android 中翻转 ImageButton

php - 如何使用php、mysql更新数据库

java - 更改图像按钮处于保留状态

android - java.util.ConcurrentModificationException?

java - Android MediaCodec,解码后的数据可以是原始大小的两倍吗?

android - 在 onPictureTaken 中处理原始图像数据

Android 2.1 SDK 在可用包列表中不可用

android - 如何设置与操作栏相同的渐变可绘制状态栏?

android - 三星上的资源$NotFoundException