图层按钮来自图层列表中的drawable?

时间:2011-10-29 05:26:19

标签: android drawable imagebutton layer

我已经找了几个小时来回答这个看似简单的问题。

基本上,你如何在图层列表中创建一个可绘制的图像按钮?

Eclipse在运行时没有标记此代码有任何错误我关闭了一个强制关闭。我错过了什么?

另外 -

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

XML文件中的android:id是否属于item标记或bitmap标记?

编辑:这段代码是我快速拼凑起来展示我的问题的代码。我正在努力使最后一个分层drawable活动按钮。例如,对于UI,我将一堆或图形叠加在一起,最后两层将是我想要激活的按钮。那可能吗?或者也许有更好的方法来做我想做的事情?

感谢。


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 个答案:

答案 0 :(得分:1)

您不会将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);
相关问题