如何在android应用程序上为ImageButton添加广告效果?
我目前在我的android应用程序项目中添加了两个ImageButton,但当我在android设备上按下时,按钮没有缩进的效果。我怎样才能给它添加效果,使它在按下时看起来更像一个按钮 我的完整源代码如下所示:如何在android应用程序上为ImageButton添加广告效果?,android,eclipse,button,indentation,effect,Android,Eclipse,Button,Indentation,Effect,我目前在我的android应用程序项目中添加了两个ImageButton,但当我在android设备上按下时,按钮没有缩进的效果。我怎样才能给它添加效果,使它在按下时看起来更像一个按钮 我的完整源代码如下所示: final TextView text2 = (TextView) findViewById(R.id.textView2); text2.setText(""); final TextView text = (TextView) findViewById(R.i
final TextView text2 = (TextView) findViewById(R.id.textView2);
text2.setText("");
final TextView text = (TextView) findViewById(R.id.textView1);
text.setText("");
final Button button3 = (Button) findViewById(R.id.button1);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked1 = 0;
clicked2 = 0;
text2.setText(" " + clicked1 + " SHOTS ");
text.setText(" " + clicked2 + " CUPS ");
}
});
final TextView text1 = (TextView) findViewById(R.id.textView2);
text1.setText(" 0 SHOTS");
final ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked2++;
text1.setText(" " + clicked2 + " SHOTS ");
}
});
final TextView text3 = (TextView) findViewById(R.id.textView1);
text3.setText(" 0 CUPS");
final ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked1++;
text3.setText(" " + clicked1 + " CUPS ");
}
});
}
}您可以很容易地做到这一点,让我们尝试使用简单的按钮代替图像按钮,并执行以下操作
创建一个xml文件并将其保存在项目中的一个
drawable
文件夹中,让bg.xml
将此文件设置为按钮的背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/button_notpressed" />
</selector>
<Button
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:background="@drawable/bg" <--! set it like this-->
android:text="Submit"
android:textColor="#FFFFFF" />