我想知道这些州有什么不同.我没有找到任何网页澄清这一点.
Q1:有没有人设法在xml选择器中使用自定义字符串/枚举属性?我通过以下[1]获得了一个布尔属性,但不是字符串属性.
编辑:谢谢你的回答.目前android仅支持布尔选择器.请参阅接受的答案.
我打算实现一个复杂的自定义按钮,其外观取决于两个变量.其他将是布尔属性(true或false)和另一个类别属性(具有许多不同的可能值).我的计划是使用布尔和字符串(或枚举?)属性.我希望我可以使用boolean和string属性在xml选择器中定义UI.
Q2:为什么在[1]中onCreateDrawableState(),布尔属性只有在它们为真时才合并?
注意:这只是一个测试应用程序,用于确定xml选择器中是否可以使用string/enum属性.我知道我可以在没有自定义属性的情况下设置按钮的textcolor.
在我的演示应用程序中,我使用布尔属性将按钮背景设置为dark/bright和string属性以设置文本颜色,{"red","green","blue"}之一.属性定义在/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomButton">
<attr name="make_dark_background" format="boolean" />
<attr name="str_attr" format="string" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
以下是我想要实现的选择器:
@ drawable/custom_button_background(有效)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customstringattribute">
<item app:make_dark_background="true" android:drawable="@color/dark" />
<item android:drawable="@color/bright" />
</selector>
Run Code Online (Sandbox Code Playgroud)
@ color/custom_button_text_color(不起作用)
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.customstringattribute">
<item app:str_attr="red" android:color="@color/red" />
<item app:str_attr="green" android:color="@color/green" />
<item app:str_attr="blue" android:color="@color/blue" />
<item android:color="@color/grey" />
</selector>
Run Code Online (Sandbox Code Playgroud)
以下是自定义按钮背景如何连接到布尔选择器,文本颜色连接到字符串选择器.
<com.example.customstringattribute.MyCustomButton
...
android:background="@drawable/custom_button_background"
android:textColor="@color/custom_button_text_color"
...
/>
Run Code Online (Sandbox Code Playgroud)
以下是init()方法中如何加载属性:
private void init(AttributeSet attrs) {
TypedArray …Run Code Online (Sandbox Code Playgroud) 随着时间的推移,我们的Android项目已经扩展了很多,现在我们正在从同一个源代码树创建多个品牌的APK.由于Android的软件包命名要求,这已经变得具有挑战性.
我们在Android库项目中拥有所有共享代码,该项目包含在主应用程序项目中.此外,我们还为每个品牌的品牌资源提供了Android库"叠加"功能.当我们想要建立一个品牌时,我们会为该品牌添加一些额外的属性,最终在主要的共享代码Android库之前包含"覆盖"Android库.为了显示:
AppProject
Include: BrandALibrary or BrandBLibrary
Include: SharedLibrary
SharedLibrary
-> src/..
-> res/..
BrandALibrary
-> res/..
BrandBLibrary
-> res/..
Run Code Online (Sandbox Code Playgroud)
通过在构建时使用命令行开关包含.properties文件,可以在构建时切换"BrandALibrary或BrandBLibrary".没有什么太花哨的了.构建脚本还必须在构建时更改.APK的包名称,以便两个.APK文件可以同时在给定设备上共存(主要用于QA目的,因为我们不希望客户同时使用这两个.APK文件同时......虽然他们可以).
在我们的共享代码库项目中添加一个类之后,一直运行良好,直到我们的构建完成.我四处寻找下面这个网页的原因,我偶然发现了以下网页,该网站遇到了同样的问题并修复了他的版本以使其工作:
在构建期间,日志中会显示以下错误:
-pre-build:
-code-gen:
[echo] ----------
[echo] Handling aidl files...
[aidl] No aidl files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[renderscript] No renderscript files to compile.
[echo] ----------
[echo] Handling Resources...
[aapt] Found Deleted Target File
[aapt] Generating resource IDs...
[aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\layout\fancy_layout.xml:22: error: No resource identifier found for attribute 'state_add' in package 'com.myapp'
[aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\layout\fancy_layout.xml:22: error: …Run Code Online (Sandbox Code Playgroud) <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.app">
<item app:remainingUse="2" android:drawable="@drawable/blabla2" />
<item app:remainingUse="1" android:drawable="@drawable/blabla1" />
<item app:remainingUse="0" android:drawable="@drawable/blabla0" />
<item android:drawable="@drawable/blabla3" />
</selector>
Run Code Online (Sandbox Code Playgroud)
是否可以在上面的自定义选择器中使用整数值作为控件状态?
(remainingUse是attrs.xml中定义的自定义整数变量)
我有一个具有以下样式的 Edittext
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_focused="true" />
<item android:color="@color/yellow" android:state_focused="false" />
<item android:color="@color/white" android:state_active="true" />
</selector>
Run Code Online (Sandbox Code Playgroud)
在我应用的 Edittext 样式中
<item name="backgroundTint">@drawable/states_edit_text</item>
Run Code Online (Sandbox Code Playgroud)
它有效。这会更改状态 normal 和focused 中的 EditTexts 底线颜色。当它处于错误状态时,我需要将色调颜色更改为红色。
就像是
<item android:state_error="true" android:color="@color/red"></item>
Run Code Online (Sandbox Code Playgroud)
但是没有称为错误的状态,我参考了其他答案,他们建议通过代码来实现。有什么方法可以使用android样式,我可以在错误状态下将EditText色调设置为红色吗?
我见过自定义xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)
和
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/com.package.custom"
Run Code Online (Sandbox Code Playgroud)
这两个人之间有什么区别?
谢谢.
我正在尝试创建一个自定义按钮,根据按钮状态(按下,启用等)更改其阴影属性(半径,距离等)
我终于接受了使用XML选择器无法完成的操作,因此我重写了View.drawableStateChanged(),并尝试使用View.getDrawableState()来确定当前状态.
但是,这个函数返回一个int [],我无法弄清楚这个值的含义,以及如何从中提取单个状态.文档是纯粹的废话:
public final int [] getDrawableState()
在API级别1中添加
返回表示视图当前状态的可绘制状态的资源ID数组.
返回当前可绘制状态
我也没能找到在线示例,与此相关的Android源代码非常神秘.
那么,你如何从这个int []中弄清楚,按钮的当前"按下"状态是什么?还是"启用状态"?
android android-ui android-view android-resources android-drawable