我正在尝试制作自定义视图,并声明了如下样式的属性: -
<resources>
<declare-styleable name="NewCircleView">
<attr name="radius" format="integer"/>
<attr name="circlecolor" format="color"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
在customview的构造函数中,这些值如下所示: -
circleradius=a.getInt(R.styleable.NewCircleView_radius, 0);//global var
circlecolor=a.getColor(R.styleable.NewCircleView_circlecolor, 0);//global var and a is the typed array
Run Code Online (Sandbox Code Playgroud)
通过声明xml如下使用该视图: -
<com.customviews.NewCircleView
android:layout_below="@id/thetext"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:radius="10000"
app:circlecolor="@color/black"<!--this is defined in colors.xml
/>
Run Code Online (Sandbox Code Playgroud)
在自定义视图中,我将绘制对象设置为: -
thePaintObj.setColor(circlecolor);//circlecolor logs to an integer as expected
Run Code Online (Sandbox Code Playgroud)
我没有得到xml中定义的颜色 - "黑色"
但是,当我将颜色设置为
thePaintObj.setColor(Color.GRAY)
Run Code Online (Sandbox Code Playgroud)
我在视图中得到了颜色
有人能告诉我,我会做错什么?
(注意: - 如果您希望我发布更多代码,请告诉我)
EDIT1: - 发布我的colors.xml.看起来我的代码评论中不清楚: -
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#7f00</color>
<color name="blue">#770000ff</color>
<color name="green">#7700ff00</color>
<color name="yellow">#77ffff00</color>
<color name="black">#000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud) 我正在阅读Ha Duy Trung的教程在您的Android应用程序中支持多个主题,其中包含以下代码片段values/attrs.xml:
<attr name="themedMenuStoryDrawable" format="reference" />
<attr name="themedMenuCommentDrawable" format="reference" />
Run Code Online (Sandbox Code Playgroud)
这个<attr>元素实际记录在哪里?查看提供资源的Android API指南,我找不到任何关于此类元素的提及.
我确实<attr>在创建视图类中找到了元素,但是它们都出现在<declare-styleable>元素内部,并且几乎没有任何关于这些元素如何工作的解释.
该<attr>元素是否有适当的参考文档?如果没有,那么:
<attr>直接有一个元素是什么意思<resources>?format属性的含义是什么,它可以采用什么值?<attr>?<attr>作为除了<resources>和以外的任何东西的孩子出现<declare-styleable>?我特别想知道如何<attr>在主题(而不是自定义视图)的上下文中使用,尽管它的工作原理的文档会更好.
我试图弄清楚如何android:gravity通过xml 传递自定义视图.
此处发布的解决方案(/sf/answers/240939051/)表示要将该android:gravity属性添加到其他attr的.
当我这样做时,我最后会发出警告:
/Users/greg/dev/company/mobile/my_app/app/src/main/res/values/colors.xml
错误:属性"android:gravity"已经定义错误:任务':app:processStagingDebugResources'的执行失败.
com.android.ide.common.internal.LoggedErrorException:无法运行命令:/Users/greg/dev/android-sdk/sdk/build-tools/21.0.2/aapt package -f --no-crunch -I /用户/ greg/dev/android-sdk/sdk/platforms/android-21/android.jar -M/Users/greg/dev/company/mobile/myapp/app/build/intermediates/manifests/full/staging/debug/AndroidManifest.xml -S/Users/greg/dev/company/mobile/myapp/app/build/intermediates/res/staging/debug -A/Users/greg/dev/company/mobile/myapp/app/build/intermediates/assets/staging/debug -m -J/Users/greg/dev/company/mobile/myapp/app/build/generated/source/r/staging/debug -F/Users/greg/dev/company/mobile/myapp/app/build/intermediates/res/resources-staging-debug.ap_ --debug-mode --custom-package com.company.vendorreviews -0 apk --output-text-symbols/Users/greg/dev/company/mobile/myapp/app/build/intermediates/symbols/staging/debug错误代码:1输出:/Users/greg/dev/company/mobile/myapp/app/build/intermediates/res/staging/debug/values/values.xml :165:错误:属性"andr oid:gravity"已经定义了
我的colors.xml文件?
values.xml的第165行是
<declare-styleable name="StackedTextView">
<attr format="integer" name="android:gravity"/>
<attr format="string" name="line1"/>
<attr format="string" name="line2"/>
<attr format="dimension" name="line1_textSize"/>
<attr format="dimension" name="line2_textSize"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
在多个组中有多次出现android:gravity.主题,FlowLayout(包含库),LinearLayoutCompat等.
编辑:更新的错误消息.
Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString());
Run Code Online (Sandbox Code Playgroud) 嗯,这是我在任何地方都找不到的东西。它可能写在某个地方,但我的可能是由于我的搜索技巧不佳而无法找到它。
所以基本上我想要做的是,我想创建一个类,我在其中传递字符串数组,并且从该数组中,该类将返回一个视图,其中按钮的数量作为字符串数组中元素的数量。
就像是,
Public class customView extends View {
public customView(Context context, AttributeSet attrs, String[] array) {
super(context, attrs, array);
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点。因为 View 类在构造函数参数中不支持 String 数组。有没有人对此有任何解决方案?我应该采取任何新方法来实现这一目标吗?
谢谢,
杰·斯泰平。