在图层列表绘制的ImageButton? [英] Imagebutton from drawable in layer-list?

查看:122
本文介绍了在图层列表绘制的ImageButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了好几个小时的答案,我有这个看似简单的问题。

基本上,你如何在图层列表某一个绘制的ImageButton?

当它运行我得到一个强制关闭

Eclipse不纪念这个code。与任何错误呢。我在想什么?

此外 -

我用 findViewById 来引用,即使资源时,他们是在一个图层列表?

请问的android:ID XML文件中的项目标记或位图标记?

编辑:这code是只是我快速放在一起展示我的问题。我试图让刚刚过去的分层绘制活动按钮。例如,对于一个UI,我躺在彼此顶部一堆或图形,最后两层将是我要激活按钮。那可能吗?或者,也许有一个更好的办法做到我想要做什么?

感谢。


layers.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目>
      <位图的android:SRC =@绘制/ android_red
        机器人:比重=中心/>
    < /项目>
    <项目的android:顶部=10dp机器人:左=10dp>
      <位图的android:SRC =@绘制/ android_green
        机器人:比重=中心/>
    < /项目>
    <项目机器人:ID =@ + ID / blue_button
        机器人:顶部=20dp机器人:左=20dp>
      <位图的android:SRC =@绘制/ android_blue
        机器人:比重=中心/>
    < /项目>
< /层列表>


main.xml中:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < ImageView的
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        机器人:SRC =@绘制/图层/>< / LinearLayout中>


活动:

 公共类LayoutTestActivity扩展活动实现OnClickListener {      保护无效的onCreate(捆绑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(本);
      }    公共无效的onClick(视图v){
    }
}


解决方案

您不要把身份证给图层列表绘制但到了ImageView的。

 < ImageView的
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:SRC =@绘制/层
    机器人:ID =@ + ID / blue_button/>

要访问使用绘制

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

I've looked for hours for the answer to this seemingly simple issue I'm having.

Basically, how do you make a certain drawable in a layer-list an imagebutton?

Eclipse doesn't mark this code with any errors yet when it runs I get a force close. What am I missing?

Also --

Do I use findViewById to reference resources even when they're in a layer-list?

Does the android:id in the XML file belong with the item tag or the bitmap tag?

EDIT: This code is just something I put together quickly to demonstrate my issue. I'm trying to make just the last layered drawable the active button. For example for a UI I am laying a bunch or graphics on top of one another, the last two layers will be buttons that I want to make active. Is that possible? Or maybe there's a better way to do what I'm trying to do?

Thanks.


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>


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


    }
}

解决方案

You do not put the id to the layer list drawable but to the Imageview.

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

To access the drawable you use

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

这篇关于在图层列表绘制的ImageButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆