我想在点击或聚焦时更改按钮的背景图像.
这是我的代码:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText("!---- On click ----!");
}
});
Run Code Online (Sandbox Code Playgroud)
这段代码对吗?它会在事件中调用一个按钮吗?
And*_*ler 90
你可以在xml文件中实现如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
<item android:drawable="@drawable/image_name_while_notpressed" /> //means normal
</selector>
Run Code Online (Sandbox Code Playgroud)
现在将此xml文件保存在drawable文件夹中,并将其命名为supps abc.xml并将其设置如下
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Run Code Online (Sandbox Code Playgroud)
希望它会对你有所帮助.:)
Chi*_*rag 56
它很容易实现.为此,您需要创建一个xml文件(选择器文件)并将其放在res中的drawable文件夹中.之后在布局文件中的按钮背景中设置xml文件.
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/your_hover_image"/>
<item android:drawable="@drawable/your_simple_image" />
</selector>
Run Code Online (Sandbox Code Playgroud)
现在在按钮的背景中设置上面的文件.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/grey_text"
android:background="@drawable/button_background_selector"/>
Run Code Online (Sandbox Code Playgroud)
对不起这是错的.
要根据特定事件(焦点,按,正常)更改背景颜色/图像,您需要定义一个按钮选择器文件并将其实现为按钮的背景.
例如: button_selector.xml(在drawable文件夹中定义此文件)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
<!-- IF you want image instead of color then write
android:drawable="@drawable/your_image" inside the <item> tag -->
Run Code Online (Sandbox Code Playgroud)
并将其应用为:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="@drawable/button_selector.xml" />
Run Code Online (Sandbox Code Playgroud)
使用此代码在可绘制文件夹名称中创建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/buutton_pressed" />
<item
android:drawable="@drawable/button_image" />
</selector>
Run Code Online (Sandbox Code Playgroud)
并在按钮xml文件中
android:background="@drawable/button"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117197 次 |
| 最近记录: |