我item_bg_selector.xml在drawable文件夹中创建了一个状态列表选择器文件,如下所示:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/item_background_selected" />
<item android:state_focused="true"
android:drawable="@drawable/item_background_selected" />
<item android:state_pressed="true"
android:drawable="@drawable/item_background_selected" />
<item android:drawable="@android:color/transparent"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后我定义一个样式元素
<style name="Item">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/item_bg_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后我尝试创建ImageButton这样的:
<ImageButton style="@style/Item"
android:id="@id/btn_home"
android:src="@drawable/home_button" />
Run Code Online (Sandbox Code Playgroud)
Eclipse不断在Event Details窗口中给出以下错误:
Failed to parse file /path/to/projectdir/res/drawable/item_bg_selector.xml
Run Code Online (Sandbox Code Playgroud)
异常跟踪如下:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #16: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208)
at com.android.layoutlib.bridge.android.BridgeTypedArray.getDrawable(BridgeTypedArray.java:731)
at android.view.View.<init>(View.java:1885)
at android.widget.ImageView.<init>(ImageView.java:112) …Run Code Online (Sandbox Code Playgroud) 我定义了以下drawable my_background_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
<item android:drawable="@drawable/selector_png_drawable" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我还定义了以下颜色状态列表资源color_stateful.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#FF00ff00"/>
<item android:color="#FFff0000"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
当我将my_background_drawable某个视图设置为某个视图的背景时,我无法观察到color_stateful.xml为我的形状定义的颜色的任何变化,而视图状态实际上已更改(selector_png_drawable.xml是指示符).
但是,当我通过my_background_drawable.xml以下方式修改我时,一切都很好:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This doesn't work
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
-->
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="#FF00ff00" />
</shape>
</item> …Run Code Online (Sandbox Code Playgroud) 我想创建一个类似于Google+应用通知的通知图标视图.不同之处在于我需要能够在运行时更改颜色,因为Google+图标为灰色或红色,所以我假设他们正在使用StateListDrawable.
对此最好的方法是什么?我更喜欢圆角的夹角,并且可以选择内部有一个可绘制的.此自定义视图也将放置在操作栏中.我仍然需要视图响应android:背景状态列表drawables所以我可以点击和选择按照工作.
此自定义视图也将放置在操作栏中.

我正在尝试添加到我现有的statelist drawable,一个禁用状态,它只是不起作用.
最初,我有这个代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/store_item_background_selected" android:state_selected="true"/>
<item android:drawable="@drawable/store_item_background"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
它适用于选定而未选中.
现在我想添加android:state_enabled ="false",如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/store_item_background_selected" android:state_selected="true"/>
<item android:drawable="@drawable/store_item_background" android:state_enabled="true"/>
<item android:drawable="@drawable/store_item_background_disabled"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
它永远不会切换到禁用的图像.
有任何想法吗?
编辑
我添加setEnabled(false)到视图的构造函数中,我将此状态列表设置为drwable,现在我看到禁用的图像,但是一旦我将视图设置为启用,它就不会再次切换到禁用状态.
我有一个状态列表drawable,我想从状态列表drawable得到一个特定的drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:kplus="http://schemas.android.com/apk/res-auto">
<item kplus:key_type_space_alt="true" android:state_pressed="true" android:drawable="@drawable/space_1_pressed" />
<item kplus:key_type_space_alt="true" android:drawable="@drawable/space_1_normal" />
<!-- TopNav keys. -->
<item kplus:key_type_topnav="true" android:state_pressed="true" android:drawable="@drawable/tab_down" />
<item kplus:key_type_topnav="true" android:state_selected="true" android:drawable="@drawable/tab_down" />
<item kplus:key_type_topnav="true" android:drawable="@drawable/tab_normal" />
<!-- TopRow keys. -->
<item kplus:key_type_toprow="true" android:state_pressed="true" android:drawable="@drawable/numeric_presseed" />
<item kplus:key_type_toprow="true" android:drawable="@drawable/numeric_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我为每个键选择了正确的可绘制状态,如下所示:
if (keyIsNumbers) {
if (KPlusInputMethodService.sNumbersState == 2) {
drawableState = mDrawableStatesProvider.KEY_STATE_TOPNAV_CHECKED;
}
}
Run Code Online (Sandbox Code Playgroud)
现在状态定义如下:
KEY_STATE_TOPNAV_NORMAL = new int[] {keyTypeTopNavAttrId};
KEY_STATE_TOPNAV_PRESSED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_pressed};
KEY_STATE_TOPNAV_CHECKED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_selected}; …Run Code Online (Sandbox Code Playgroud) android android-layout android-input-method statelistdrawable android-drawable
在我的应用程序中,我有一个包含列表视图的左窗格.在此列表视图中所做的选择,然后调用相关数据,这将更新右侧窗格中,并在活动的其他环节和要素.由于此设计,左窗格必须始终进行选择,以便为活动的其余部分提供有用的值.
在我的活动的onCreate方法中,我调用以下内容以默认选择列表中的第一个元素.(programList是我的listview)
programList.performItemClick(programList, 0, programList.getItemIdAtPosition(0);
Run Code Online (Sandbox Code Playgroud)
除了一件事以外,一切都很好,视图加载正确的值和正确的值.
我为listview添加了一个drawable选择器,它突出显示了所选元素.在模拟器中物理单击该项时,它将突出显示(我正在使用CHOICE_MODE_SINGLE),但在调用performItemClick()时它不会突出显示.我在stackoverflow上寻找帮助,并尝试在点击之前使用以下内容,但它没有任何区别.
programList.requestFocusFromTouch();
programList.setSelection(0);
Run Code Online (Sandbox Code Playgroud)
通过阅读API,听起来像setSelection方法没有在触摸界面下设置项目的状态,从我的结果看来,似乎performItemClick方法也没有.我也试过用
programList.getAdapter().getView(0, null, null).setSelected(true);
Run Code Online (Sandbox Code Playgroud)
然后我可以读取文本视图的状态并看到它被选中,但是这也没有改变屏幕上显示的内容.
onCreateMethod中是否有某些东西阻止按钮的状态被不同地绘制?我尝试使单个文本视图中的drawable无效并使文本视图本身无效,但这没有帮助.我也尝试更改文本视图的背景,但似乎我设置的选择器控制或在前面绘制.我知道我有正确的textView因为我使用了getText来读取里面的内容.
如果有人能够帮助我解决这个问题或指出我的解决方案,我将不胜感激.
我有一个ToggleButton.我希望背景清晰,就像一周中几天的默认闹钟应用程序一样.下面的代码涵盖了清晰颜色的切换.有没有办法保持切换和更改背景颜色而不滚动我自己的切换按钮?如果没有,整个过程都会非常糟糕,imo.另外,我真的必须在这里定义一个清晰的颜色,还是我可以在我的xml中使用内置的清晰颜色?
<ToggleButton
android:background="@drawable/clear_toggle_button"
android:id="@+id/btn_sunday"
android:layout_width="50dp"
android:layout_height="50dp"
android:textOn="SUN"
android:textOff="SUN"
/>
Run Code Online (Sandbox Code Playgroud)
这是我的colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="clear">#ffffff00</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是我在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="@color/clear" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/clear" />
</selector>
Run Code Online (Sandbox Code Playgroud) android android-layout statelistdrawable android-togglebutton
我创建了一个扩展ViewGroup的类.此MyCustomViewGroup类的一个功能是充当嵌套类MyButton的容器,该类扩展了Button.
我以正常方式从自定义AttributeSet设置MyCustomViewGroup的自定义属性.其中一个属性定义StateListDrawable以用于MyButton嵌套类的实例的背景.我将它存储在类变量mMyButtonBackground中.
public class MyCustomViewGroup extends ViewGroup {
private Drawable mMyButtonBackground;
...
Run Code Online (Sandbox Code Playgroud)
每次我在MyCustomViewGroup中创建MyButton的新实例时,我都会设置它的背景.
MyButton myButton = new MyButton(context);
myButton.setBackground(mMyButtonBackground);
Run Code Online (Sandbox Code Playgroud)
在运行时,StateListDrawable似乎只适用于最近添加的MyButton实例.
例如,假设我在MyCustomViewGroup中创建了4个MyButton实例.如果我单击MyButton number 4,它的背景会发生变化,如StateListDrawable中所定义.如果我点击MyButton 1到3,他们的背景不会改变,但MyButton编号为4.
从逻辑上讲,这表明它是一个可变性问题.所有MyButton实例共享存储在mMyButtonBackground中的相同StateListDrawable.考虑到这一点,我尝试过:
MyButton myButton = new MyButton(context);
Drawable myButtonBackground = mMyButtonBackground.mutate();
myButton.setBackground(myButtonBackground);
Run Code Online (Sandbox Code Playgroud)
但这并没有解决问题.我也试过将它专门用作StateListDrawable:
MyButton myButton = new MyButton(context);
StateListDrawable myButtonBackground = (StateListDrawable)mMyButtonBackground.mutate();
myButton.setBackground(myButtonBackground);
Run Code Online (Sandbox Code Playgroud)
这也没有解决问题.在我试图解决这个问题的研究中,我已经阅读了Romain Guy关于Drawable突变的这篇文章.我原以为,由于StateListDrawable是Drawable的子类,我应该可以应用相同的方法,但我似乎无法让它工作.我错过了什么?
我有一个自定义按钮,并通过编程方式更改其按下和默认颜色.
public class CustomApplicationButton extends Button {
public CustomApplicationButton(Context context) {
this(context, 0, 0, 0);
}
public CustomApplicationButton(Context context, int topDrawableResId, int outlineDefaultColorId, int outlinePressedColorId) {
super(context);
// set width and height
LinearLayout.LayoutParams params = new LayoutParams(
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_width),
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_height));
setLayoutParams(params);
// set drawable top icon
if (topDrawableResId != 0) {
setCompoundDrawablesWithIntrinsicBounds(0, topDrawableResId, 0, 0);
}
// set background and outline color
int strokeWidth = context.getResources().getDimensionPixelSize(R.dimen.sr_launcher_button_stroke_size);
// unpressed state drawable
LayerDrawable defaultLayers = (LayerDrawable) context.getResources().getDrawable(
R.drawable.btn_launcher_shape_default);
GradientDrawable defaultShapeOutline = (GradientDrawable) defaultLayers.findDrawableByLayerId(R.id.outline_default); …Run Code Online (Sandbox Code Playgroud) 我正在尝试以编程方式将a设置StateListDrawable为库项目的自定义视图的背景.这是我正在做的事情:
final TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.ActionBar);
int firstColor = a.getColor(
R.styleable.ActionBar_backgroundGradientFirstColor, 0xff000000);
int secondColor = a
.getColor(R.styleable.ActionBar_backgroundGradientSecondColor,
0xff000000);
int textViewColor = a.getColor(R.styleable.ActionBar_titleColor,
0xffffffff);
int onClickColor = a.getColor(
R.styleable.ActionBar_backgroundClickedColor, 0xff999999);
a.recycle();
StateListDrawable sld = new StateListDrawable();
GradientDrawable drawable = new GradientDrawable(
Orientation.TOP_BOTTOM, new int[] { firstColor, secondColor });
sld.addState(new int[] { android.R.attr.state_enabled },
new ColorDrawable(onClickColor));
sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
action2.setBackgroundDrawable(sld);
action3.setBackgroundDrawable(sld);
actionBack.setBackgroundDrawable(sld);
pb.setBackgroundDrawable(drawable);
tv.setBackgroundDrawable(drawable);
tv.setTextColor(textViewColor);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用:它总是绘制启用状态.我希望它来绘制按下时,我的状态按下按钮.我究竟做错了什么?
似乎StateListDrawable将忽略应用于它们包含的drawable的颜色过滤器.例如:
StateListDrawable sld = new StateListDrawable();
Drawable pressedState = Context.getResources().getDrawable(R.drawable.solid_green);
pressedState.setColorFilter(Color.RED, PorterDuff.Mode.SRC);
sld.addState(new int[] {android.R.attr.state_pressed}, pressedState);
// Other states...
Run Code Online (Sandbox Code Playgroud)
如果应用于sld视图的背景,则可以预期视图的背景在按下时会变为红色.相反,它会变成绿色 - pressedState没有应用过滤器的颜色.
我ImageView在 XML 中定义如下:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_imageView"
android:src="@drawable/ic_photo"
android:background="@drawable/button_state_list_drawable" />
Run Code Online (Sandbox Code Playgroud)
该button_state_list_drawable.xml是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_pressed_state_drawable"
android:state_pressed="true" />
<item android:drawable="@drawable/button_enabled_state_drawable"
android:state_enabled="true" />
</selector>
Run Code Online (Sandbox Code Playgroud)
该button_pressed_state_drawable.xml是:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size android:width="50dp"
android:height="50dp" />
<padding android:left="25dp"
android:right="25dp"
android:top="25dp"
android:bottom="25dp" />
<stroke android:color="#fff"
android:width="1dp" />
<solid android:color="#1aafd0" />
</shape>
Run Code Online (Sandbox Code Playgroud)
该button_enabled_state_drawable.xml是:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size android:width="50dp"
android:height="50dp" />
<padding android:left="25dp"
android:right="25dp"
android:top="25dp"
android:bottom="25dp" />
<stroke android:color="#fff" …Run Code Online (Sandbox Code Playgroud) android imageview xml-drawable statelistdrawable android-drawable
StateListDrawable在 Jetpack Compose 中获得 a 功能的最佳方法是什么?
android ×13
xml-drawable ×2
colorfilter ×1
colors ×1
custom-view ×1
imagebutton ×1
imageview ×1
kotlin ×1
layout ×1
listview ×1
selection ×1
stroke ×1