Android:自定义视图属性上的选择器(Android: Selector on custom View Attribute)
我试图了解本指南: 如何添加自定义按钮状态 ,创建自己的视图属性,以及使用选择器来更改其状态。 我似乎无法让它发挥作用。 按钮选择器在非自定义选择器上工作正常,例如按下按钮,但不适用于我的costum选择器。 我的代码如下:
在attrs.xml中:
<resources> <declare-styleable name="ValueButton"> <attr name="toggle" format="boolean" /> </declare-styleable> </resources>
在我的自定义按钮类定义文件中,名为ValueButton.java:
public class ValueButton extends Button { private static final int[] STATE_TOGLLE = {R.attr.toggle}; private boolean toggle = false; public void setToggle(boolean val) { toggle = val; } public ValueButton(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); if(toggle) mergeDrawableStates(drawableState,STATE_TOGLLE); return drawableState; } }
在我看来,使用按钮:
<LiniarLayout> <com.myapp.ValueButton android:id="@+id/rightText" custom:toggle="false" android:layout_height="wrap_content" android:layout_width="wrap_content" style="@style/ValueSwitchStyle" /> </LiniarLayout>
在我的styles.xml文件中:
<style name="ValueSwitchStyle"> <item name="android:background">@drawable/value_switch_background</item> </style>
最后是我的后台定义文件(button_background.xml),位于drawables文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/com.myapp.ValueButton"> <item custom:toggle="true" android:drawable="@color/blue"/> <item custom:toggle="false" android:drawable="@color/white"/> </selector>
I have tried to folow this guide: How to add a custom button state, to create my own view attribute, and using an selector to change its state. I can't seem to get it to work. The selector for button works fine on non-custom selectors, such as button pressed, but doesn't work for my costum selector. My code is as follows:
in attrs.xml:
<resources> <declare-styleable name="ValueButton"> <attr name="toggle" format="boolean" /> </declare-styleable> </resources>
In my custom button class definition file, called ValueButton.java:
public class ValueButton extends Button { private static final int[] STATE_TOGLLE = {R.attr.toggle}; private boolean toggle = false; public void setToggle(boolean val) { toggle = val; } public ValueButton(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); if(toggle) mergeDrawableStates(drawableState,STATE_TOGLLE); return drawableState; } }
In my view that uses the button:
<LiniarLayout> <com.myapp.ValueButton android:id="@+id/rightText" custom:toggle="false" android:layout_height="wrap_content" android:layout_width="wrap_content" style="@style/ValueSwitchStyle" /> </LiniarLayout>
in my styles.xml file:
<style name="ValueSwitchStyle"> <item name="android:background">@drawable/value_switch_background</item> </style>
and finally my background definitions file (button_background.xml), located in the drawables folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/com.myapp.ValueButton"> <item custom:toggle="true" android:drawable="@color/blue"/> <item custom:toggle="false" android:drawable="@color/white"/> </selector>
原文:https://stackoverflow.com/questions/26690688
最满意答案
data = File.read("/path/to/file")
Answering my own question here... turns out it's a Windows only quirk that happens when reading binary files (in my case a JPEG) that requires an additional flag in the open or File.open function call. I revised it to
open("/path/to/file", 'rb') {|io| a = a + io.read}
and all was fine.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
将文件放在FTP站点上,内容来自字符串变量(无本地文件)(Put a file on FTP site with contents from string variable (no local file))[2022-09-18]
这可能不是最终的解决方案,但我认为还是比原来的方法更好: 您可以通过使用PHP内存流来避免文件系统上的临时文件。 它基本上是一个文件句柄包装(后台)不使用实际文件,而是使用一些内存。 所以实际上你仍然使用一个文件句柄 (所以ftp_fput很开心),但是没有实际的文件 (所以没有文件写入磁盘并且开销保持最小)。 $tmpfile = fopen('php://memory', 'r+'); fputs($tmpfile, $content); rewind($tmpfile); // or fseek 请 ... -
data = File.read("/path/to/file") Answering my own question here... turns out it's a Windows only quirk that happens when reading binary files (in my case a JPEG) that requires an additional flag in the open or File.open function call. I revised it to ope ...
-
通过读取另一个文件的内容,在makefile中创建一个变量(Create a variable in a makefile by reading contents of another file)[2021-10-22]
我猜你想在Makefile中设置一个变量到另一个文件的内容: FILE=test.txt VARIABLE=`cat $(FILE)` target: echo $(VARIABLE) I'm guessing that you like to set a variable in your Makefile to the contents of another file: FILE=test.txt VARIABLE=`cat $(FILE)` target: echo $(VARIA ... -
将文件内容读入变量中: for /f "delims=" %%x in (version.txt) do set Build=%%x 要么 set /p Build=
如何将文件内容读入qmake变量并将其传递给编译器?(How to read file content into a qmake variable and pass it to the compiler?)[2023-02-09]
在所有平台上,都有一个内置的替换函数$$cat : # set a qmake variable KEY = "$$cat(/path/to/the/file)" # propagate the variable to C/C++ DEFINES += "KEY=\"$$KEY\"" 该行的效果与在每个翻译单元的开头添加以下行相同: #define KEY假设该文件包含一行: FOO 然后: // source qDebug() << KEY; / ... 两者都做同样的...它只是归结为味道。 Both do the same.. it just comes down to taste.是的,这是一个棘手的方法。 把东西放到props.txt并运行脚本: DECLARE -- the @@ expression must be in separate line as follows file_contents VARCHAR2(32767) := ' @@props.txt '; BEGIN dbms_output.put_line('==='); dbms_output.put_line(file_contents); dbms_output.put_line('= ...你能让他们允许Ad Hoc Distributed Queries吗? 然后您可以使用OpenRowset或OpenDatasource 。 SELECT * FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=c:\users\graham\desktop;', 'SELECT * FROM [data.txt];' 如果需要,这是重新配置的代码: EXEC sp_confi ...相关文章
更多- Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案
- JMS&ActiveMQ实战- Selector
- JMS&ActiveMQ实战- JMSCorrelationID与Selector
- Hadoop0.20+ custom MultipleOutputFormat
- Android编程之仿微信显示更多文字的View
- Android编程之仿微信显示更多文字的View
- [译文] 恶意软件行为综述 - A View on Current Malware Behaviors
- [15] 微信公众帐号开发教程第15篇-自定义菜单的view类型(访问网页)
- solr 使用自定义的 QueryParser
- Custom SOLR Search Components - 2 Dev Tricks
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。