c语言常见问题问题

android - In my listview Failed to convert @null into a ColorStateList - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
J it only takes a minute:
when I try implement listView in my layout I am getting this "
Failed to convert @null into a ColorStateList
" message in my graphical layout view. what is this? how to resolve this error?
//when I remove the list view its working/showing fine.
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" &
android:id="@+id/dropbox_sync_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"&
&/ListView&
&Button android:id="@+id/add_folder_dropbox"
android:layout_width="fill_parent"
android:layout_margin="15dip"
android:layout_height="wrap_content"
android:background="@drawable/login_blue"
android:textColor="@color/white"
android:textSize="18dip"
android:textStyle="bold"
android:text="+ Add Folder from Dropbox"/&
&/LinearLayout&
15.4k145484
To your root LinearLayout add xmlns:android="/apk/res/android". Does that help?
4,812114786
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25426
Stack Overflow works best with JavaScript enabledAndroid开发(1087)
在Android开发者中,对图片的使用是必不可少的,有时候不会切图,有时候是因为图标过多导致应用程序包过大等等,常见的就是在图标的几种状态,一般都是默认和选中两种,我们给图片着色来让它只用一张图标可以配置任何我们想要的图标颜色,我们这里提到一个小技巧来处理这些事情,我们尽可能的写一些代码来改变这种情况。
在官方的做法一般是我们会去像下面那样写资源文件通过配置不同的图片来解决来标明在不同状态下对应显示的图标
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"&
android:state_pressed="true"
android:drawable="@drawable/button_pressed" /&
android:state_focused="true"
android:drawable="@drawable/button_focused" /&
android:state_hovered="true"
android:drawable="@drawable/button_focused" /&
android:drawable="@drawable/button_normal" /&
但是有时候我们需要动态改变图标颜色,而这个图标颜色又比较多,当然把每种颜色都切出来很显然不显示。只能通过写代码给图标着色来解决。
提起着色,Android SDK给我们提供了一个这样的类:ColorStateList,它用来设置在不同状态下对应的颜色,经常做法如下:
xmlns:android="/apk/res/android"&
android:state_focused="true" android:color="@color/testcolor1"/&
android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" /&
android:state_enabled="false" android:color="@color/testcolor3" /&
android:color="@color/testcolor5"/&
我们可以以此入手:
继承ImageView。
自定义属性colorStateList,写selector文件。
xmlns:android="/apk/res/android"&
android:color="@color/colorPrimary" android:state_selected="true"/&
android:color="@color/colorAccent"
重写drawableStateChanged()方法:官网上说当drawable的状态在改变时会调用此方法。
调用setColorFilter()方法为图片着色
完整代码:
public class TintStateImage extends ImageView{
private static final int IMAGE_DEFAULT_COLOR = Color.GRAY;
private ColorStateList mColorStateL
public TintStateImage(Context context) {
this(context,null);
public TintStateImage(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public TintStateImage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TintStateImage);
mColorStateList = ta.getColorStateList(R.styleable.TintStateImage_colorStateList);
ta.recycle();
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mColorStateList != null && mColorStateList.isStateful()){
int color = mColorStateList.getColorForState(getDrawableState(),
IMAGE_DEFAULT_COLOR);
super.setColorFilter(color, PorterDuff.Mode.SRC_IN);
&com.yuxingxin.library.TintStateImage
android:id="@+id/selected_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorStateList="@drawable/image_style"
android:src="@mipmap/ic_home_black_24dp"
别忘了给你的view设置状态就可以了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:8227871次
积分:63322
积分:63322
排名:第27名
原创:522篇
转载:968篇
评论:2172条
如果您认为本博客不错,读后觉得有收获,不妨打赏赞助我一下,让我有动力继续写出高质量的博客。
赠人玫瑰,手有余香。分享技术,传递快乐。
QQ交流群:
有心课堂会员,请加入VIP QQ交流群:
文章:28篇
阅读:74100
文章:69篇
阅读:307446Android技巧之ColorState实践篇 - 推酷
Android技巧之ColorState实践篇
在Android开发者中,对图片的使用是必不可少的,有时候不会切图,有时候是因为图标过多导致应用程序包过大等等,常见的就是在图标的几种状态,一般都是默认和选中两种,我们给图片着色来让它只用一张图标可以配置任何我们想要的图标颜色,我们这里提到一个小技巧来处理这些事情,我们尽可能的写一些代码来改变这种情况。
在官方的做法一般是我们会去像下面那样写资源文件通过配置不同的图片来解决来标明在不同状态下对应显示的图标
&?xml version=&1.0& encoding=&utf-8&?&&selector xmlns:android=&/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:state_hovered=&true&
android:drawable=&@drawable/button_focused& /& &!-- hovered --&
&item android:drawable=&@drawable/button_normal& /& &!-- default --&&/selector&
但是有时候我们需要动态改变图标颜色,而这个图标颜色又比较多,当然把每种颜色都切出来很显然不显示。只能通过写代码给图标着色来解决。
提起着色,Android SDK给我们提供了一个这样的类:ColorStateList,它用来设置在不同状态下对应的颜色,经常做法如下:
&selector xmlns:android=&/apk/res/android&&
&item android:state_focused=&true& android:color=&@color/testcolor1&/&
&item android:state_pressed=&true& android:state_enabled=&false& android:color=&@color/testcolor2& /&
&item android:state_enabled=&false& android:color=&@color/testcolor3& /&
&item android:color=&@color/testcolor5&/& &/selector&
我们可以以此入手:
继承ImageView。
自定义属性colorStateList,写selector文件。
&selector xmlns:android=&/apk/res/android&&
&item android:color=&@color/colorPrimary& android:state_selected=&true&/&
&item android:color=&@color/colorAccent&
/&&/selector&
重写drawableStateChanged()方法:官网上说当drawable的状态在改变时会调用此方法。
调用setColorFilter()方法为图片着色
4. 附上代码:
public class TintStateImage extends ImageView{
private static final int IMAGE_DEFAULT_COLOR = Color.GRAY;
private ColorStateList mColorStateL
public TintStateImage(Context context) {
this(context,null);
public TintStateImage(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public TintStateImage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TintStateImage);
mColorStateList = ta.getColorStateList(R.styleable.TintStateImage_colorStateList);
ta.recycle();
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mColorStateList != null && mColorStateList.isStateful()){
int color = mColorStateList.getColorForState(getDrawableState(),
IMAGE_DEFAULT_COLOR);
super.setColorFilter(color, PorterDuff.Mode.SRC_IN);
最后我们在用的时候:
&com.yuxingxin.library.TintStateImage
android:id=&@+id/selected_state&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
app:colorStateList=&@drawable/image_style&
android:src=&@mipmap/ic_home_black_24dp&
别忘了给你的view设置状态就可以了。附上
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致android - Resource is not a ColorStateList exception - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
J it only takes a minute:
I did a search and found several questions on the topic, but none were good for me.
I'm trying to attach a styled color to a textview, and it keeps failing
tehilim_row.xml
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
&com.karriapps.tehilimlibrary.utils.TehilimTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/titleColor"
android:padding="10dp"
android:textSize="16sp"
android:text="@string/yehiTitle"
android:layout_gravity="@integer/title_gravity"
&com.karriapps.tehilimlibrary.utils.TehilimTextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="start"
android:text="@string/yehiBefore"
android:padding="10dp"
android:textColor="?attr/text_color"
&/LinearLayout&
&attr name="text_color" format="reference"/&
&item name="text_color"&@color/text_color&/item&
text_color.xml
&?xml version="1.0" encoding="utf-8"?&
&selector xmlns:android="/apk/res/android"&
&item android:state_focused="true" android:color="#000000"/&
&item android:state_pressed="true" android:state_enabled="false" android:color="#000000" /&
&item android:state_enabled="false" android:color="#000000" /&
&item android:color="#000000"/&
&/selector&
Exception log
04-25 09:04:10.857: E/AndroidRuntime(3357): FATAL EXCEPTION: main
04-25 09:04:10.857: E/AndroidRuntime(3357): Process: com.karriapps.tehilimlibrary, PID: 3357
04-25 09:04:10.857: E/AndroidRuntime(3357): android.view.InflateException: Binary XML file line #43: Error inflating class
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at com.karriapps.tehilimlibrary.MainFragment$TehilimAdapter.getView(MainFragment.java:199)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.AbsListView.obtainView(AbsListView.java:2263)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.ListView.onMeasure(ListView.java:1175)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:639)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.View.measure(View.java:16497)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.Choreographer.doFrame(Choreographer.java:544)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.os.Handler.handleCallback(Handler.java:733)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.os.Handler.dispatchMessage(Handler.java:95)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.os.Looper.loop(Looper.java:136)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.app.ActivityThread.main(ActivityThread.java:5017)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at java.lang.reflect.Method.invokeNative(Native Method)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at java.lang.reflect.Method.invoke(Method.java:515)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at dalvik.system.NativeStart.main(Native Method)
04-25 09:04:10.857: E/AndroidRuntime(3357): Caused by: java.lang.reflect.InvocationTargetException
04-25 09:04:10.857: E/AndroidRuntime(3357):
at java.lang.reflect.Constructor.constructNative(Native Method)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-25 09:04:10.857: E/AndroidRuntime(3357):
... 45 more
04-25 09:04:10.857: E/AndroidRuntime(3357): Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f010075 a=-1}
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.content.res.Resources.loadColorStateList(Resources.java:2237)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.content.res.TypedArray.getColorStateList(TypedArray.java:343)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.TextView.(TextView.java:960)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at android.widget.TextView.(TextView.java:618)
04-25 09:04:10.857: E/AndroidRuntime(3357):
at com.karriapps.tehilimlibrary.utils.TehilimTextView.(TehilimTextView.java:14)
04-25 09:04:10.857: E/AndroidRuntime(3357):
... 48 more
Know someone who can answer? Share a link to this
via , , , or .
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Browse other questions tagged
rev .25426
Stack Overflow works best with JavaScript enabledAndroid技术(76)
优点:灵活,减少xml的编写。应用在TextView的文字时,亦避免使用了OnTouchListener。
用途:动态设置TextView、Button、ImageView等组件在不同状态下的背景/前景显示效果。
[AndroidOpenSource]\frameworks\base\core\\\view\view.xml
[AndroidOpenSource]\frameworks\base\core\res\res\values\public.xml
效果图如下:
代码如下:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:44904次
积分:1319
积分:1319
排名:千里之外
原创:83篇
转载:36篇
(12)(13)(12)(3)(3)(6)(8)(5)(14)(5)(8)(8)(6)(7)(2)(1)(1)

我要回帖

更多关于 c语言常见问题 的文章

 

随机推荐