对于我遇到的这个看似简单的问题,我已经寻找了几个小时的答案。
基本上,您如何使图层列表中的某个可绘制对象成为图像按钮?
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/