所以我是 android 开发的新手...我如何创建一个像按钮一样的图像,所以当我按下该图像时,图像会启动一个特定的 Activity 。 所以我希望它显示为图像:
<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" />
最佳答案
创建 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);
}
};
编辑:
第二个选项,如果你想使用按钮 View 创建一个类似按钮的图像,然后创建一个自定义按钮:
首先将所有图像(如按下、聚焦和默认)放在 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 -->
/>
请参阅本教程以创建带图像的自定义按钮
关于java - 如何在android中创建图像按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176727/