【问题标题】:How to create image button in android?如何在android中创建图像按钮?
【发布时间】:2012-06-24 10:34:50
【问题描述】:

所以我是 android 开发的新手...如何创建一个像按钮一样的图像,所以当我按下该图像时,图像会启动一个特定的活动。 所以我希望它显示为图像:

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="33dp"
    android:text="Button" />

【问题讨论】:

标签: java android button android-activity imagebutton


【解决方案1】:

创建 ImageButton 为:

在 main.xml 中:

<ImageButton android:id="@+id/ib"
    android:src="@drawable/bookmark" <-- SET BUTTON IMAGE HERE -->
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

在代码部分:

ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
    }
    private OnClickListener ibLis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //START YOUR ACTIVITY HERE AS
             Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
             startActivity(intent, 0);
        }
    };

编辑:

第二个选项,如果你想使用按钮视图创建一个类似按钮的图像,然后创建一个自定义按钮:

首先把你所有的图片,比如pressed、focused和default放在res/drawable文件夹中,然后在drawable/newbtn.xml中添加一个newbtn.xml:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"  
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->  
    <item android:state_focused="true"  
          android:drawable="@drawable/button_focused" /> <!-- focused -->  
    <item android:drawable="@drawable/button_normal" /> <!-- default -->  
</selector>

最后在按钮 XML 中设置 android:background 为:

<Button    
    android:id ="@+id/btn"  
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:text="Hello"  
    android:textColor="#ffffffff"  
    android:background="@drawable/newbtn"   <-- get button background to selector -->
    /> 

请参阅本教程以使用图像创建自定义按钮

Creating custom, fancy buttons in Android

【讨论】:

  • @user1414682:确保您已在清单中将第二个活动注册为
  • 我发现了问题:我在代码中有 android:text="Button" 和 android:src="@drawable/bookmark",所以我删除了“text” 谢谢!
  • 再问一个问题,我可以使用动画图像吗?
  • 您可以使用AnimationDrawable 为您的图像按钮设置动画,但我不确定。所以只需在谷歌上搜索 AnimationDrawable 也许这会对你有所帮助。谢谢。
【解决方案2】:

使用 ImageView 元素,为其附加点击侦听器。

XML:

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

代码:

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
imageView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ThisActivity.this, MyOtherActivity.class);
        startActivity(intent);
    }
});

您也可以使用 ImageButton(以相同的方式)。这几乎没有什么区别。您可以在此处查看更多详细信息。 Difference between a clickable ImageView and ImageButton

【讨论】:

    【解决方案3】:

    使用“setOnClickListener()”过于复杂。而是使用 XML 中的“onClick”属性:

    <ImageButton
        android:id="@+id/button19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:onClick="yourCallback"
        android:src="@drawable/your_image"
    />
    
    public void yourCallback(View view) 
    {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2016-08-01
      相关资源
      最近更新 更多