- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
public void setHoverEffect(final Button button,int normalImageId, int hoverImageId) {
if (button != null)
{
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{ }, getResources().getDrawable(normalImageId));
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(hoverImageId));
button.setBackgroundDrawable(stateListDrawable);
}
}
当我使用上面的代码时,只有普通图像显示为背景,当我按下按钮时,它没有显示悬停图像。
当我使用如下所示的 selector.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/btn_ok_h"> </item>
<item android:drawable="@drawable/btn_ok"> </item>
</selector>
我想动态地执行此操作,以避免为我的应用程序中的每个按钮创建选择器 xml 文件。我无法弄清楚我的代码哪里错了或者我必须指定额外属性的地方......:(
最佳答案
@KK 你做对了但有些错误
查看我的代码
public void selector(Button b,int pressed_image,int normal_image )
{
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(pressed_image));
states.addState(new int[] {}, getResources().getDrawable(normal_image));
b.setBackgroundDrawable(states);
}
您可以说此代码与您的代码相同,但存在一些差异。我写了states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(pressed_image));
states.addState(new int[] {}, getResources().getDrawable(normal_image));
之前的行
首先尝试这段代码,我已经测试了这段代码 4-5 次,然后在这里发布。我真的不知道为什么更改行代码可以正常工作。
关于android - 如何使用 StateListDrawable 将图像设置为按钮的背景(动态或以编程方式或通过代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132047/
所以我制作了这两个按钮。 我希望能够单击我制作的按钮,并将按钮更改为原始按钮的白色图像,而不是切换按钮中的打开和关闭状态。 到目前为止,我所知道的只是与 stateListDrawable xml 有
我正在创建一个 android 应用程序,它从互联网上动态下载图像,然后将它们放置到 ImageView 上,允许用户设置默认图像和突出显示的图像。为此,我下载数据并将其保存到一个文件中,每当需要图像
我正在尝试以编程方式将 StateListDrawable 设置为库项目的自定义 View 的背景。这是我正在做的: final TypedArray a = getContext().obtainS
我有一个自定义按钮,并以编程方式更改了它的按下颜色和默认颜色。 public class CustomApplicationButton extends Button { public CustomA
我在 res/drawable/my_background.xml 中定义了一个 drawable。 my_background.xml 是:
这是我的自定义选择器(StateListDrawable) common_cell_background 和 common_cell_background_
我想创建自定义按钮以在 TabHost 中使用。我一直在尝试只使用相同的图像资源 (png),但根据状态改变滤色器。所以我做了这个作为自定义按钮的布局: 在我的 Activi
我有一个内置于 ListActivity 中的 listView。我希望在按下项目时改变我的行的背景。 所以我在我的 ListActivity 中写道:this.getListView().setSe
我有一个 GridView 来显示一些对象,并且在视觉上每个对象都会有一个图像图标和一个文本标签。我还希望图像图标在单击时具有一些“推送和弹出”效果,即按下时,图像会向右下方向移动一小段距离,释放时会
我正在尝试制作一个通用框架函数,当按下/聚焦/选择/等时,使任何 Drawable 都高亮显示。 我的函数接受一个 Drawable 并返回一个 StateListDrawable,其中默认状态是 D
假设我有一个 StateListDrawable。我想在其中添加 2 个可绘制对象:第一个如果被单击,第二个用于任何其他状态。 我试过如下(伪代码): Drawable clickdDrawable
似乎 StateListDrawable 将忽略应用于它们包含的可绘制对象的颜色过滤器。例如: StateListDrawable sld = new StateListDrawable(); Dra
它们具有按下、聚焦、禁用等状态,但是否可以创建自定义状态以反射(reflect)我自己的一些自定义条件? 例如,假设您有一个姓名列表。有些人属于某个类别,应该在视觉上以不同的方式表示。然后,这些人的行
我有以下按钮选择器(有 2 种状态,常规和按下): 我的按钮有以下指定背景作为上面的选择器: 我需要在加载 Activit
我正在使用以下代码- StateListDrawable states2 = new StateListDrawable(); states2.clearColorFilter();
我有以下按钮选择器(有 2 种状态,常规状态和按下状态): 我的按钮具有以下内容,将背景指定为上述选择器: 当 Activ
我在自定义 View 上遇到了 StateListDrawable 的大问题。我有一个直接从 LinearLayout 继承的自定义 View ,在它的 XML 布局中,背景是一个简单的状态列表可绘制
我需要访问 ClipDrawable设置剪辑电平。这是可绘制对象: @drawable/left_arrow: @drawable/rotated_square:
这是我的代码: public void setHoverEffect(final Button button,int normalImageId, int hoverImageId) {
我有一个 ListView ,其中列表项具有以下可绘制背景 当按下列表项时,它会应用渐变。然而,如果用户随后发起了一个多点触摸事件(比如在 ListView 之外的某处放置了一
我是一名优秀的程序员,十分优秀!