Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 默认情况下始终设置自定义样式属性_Android_Android Styles - Fatal编程技术网

Android 默认情况下始终设置自定义样式属性

Android 默认情况下始终设置自定义样式属性,android,android-styles,Android,Android Styles,我已经为颜色定义了自定义样式属性 <declare-styleable name="CustomView"> <attr name="custom1" format="boolean" /> <attr name="custom2" format="reference|color" /> </declare-styleable> 奇怪的问题。我只能建议您添加调试日志输出,并对更改进行实验,以便更清楚地了解正在发生的

我已经为颜色定义了自定义样式属性

<declare-styleable name="CustomView">
        <attr name="custom1" format="boolean" />
        <attr name="custom2" format="reference|color" />
</declare-styleable>

奇怪的问题。我只能建议您添加调试日志输出,并对更改进行实验,以便更清楚地了解正在发生的事情

这些语句转储自定义视图的属性ID,以及布局XML中自定义视图实例的
AttributeSet
中的属性ID

Log.i("Test", "----");
for (int id : R.styleable.CustomView) {
    Log.i("Test", "CustomView= " + id);
}

Log.i("Test", "----");
int n = attrs.getAttributeCount();
for (int i = 0; i < n; i++) {
    Log.i("Test", "AttributeSet= " + attrs.getAttributeNameResource(i) + " " + attrs.getAttributeName(i));
}

对我来说,问题是:

  private fun init(attrs: AttributeSet?) { 
    post {
        val attributes = context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.CustomButton,
            0, 0
        )
        .....
如果我在post{}中得到属性,它就不会得到。这起到了作用:

 private fun init(attrs: AttributeSet?) {
    val attributes = context.theme.obtainStyledAttributes(
        attrs,
        R.styleable.CustomButton,
        0, 0
    )
    post {
    ....

我无法复制你的结果。您是否尝试过使用XML中定义的一个或两个属性创建CustomView实例,以查看这些情况下
getIndexCount()
返回的内容?它总是会为颜色获取一些值。。因此,当仅定义custom1时,它将同时获取custom1和custom2的值。。当custom1和custom2都被定义时,大小为2,它正确地获取值。。。当只定义custom2时,大小为1,只提取颜色值。CustomView从哪个类派生?它从TextView派生。请尝试。。同时添加了另一个布尔值custom3并将其设置为false,我不需要默认颜色值。属性中没有custom2。。但是当调用ActainStyleDatAttributes时,它会以某种方式为custom2获取值。。请参见问题末尾的输出。如果您在调用
ActainStyledAttributes()
的地方发布整个代码块,并对
类型Darray
进行操作,我会仔细查看。这是一个相当大的类。。无法发布整个类。。刚刚更新了如何获取值的代码。。如果需要的话,以后可能会创建单独的项目。这是一个有趣的问题。我学到了一些东西。最后一点注意:如果您还没有这样做,请确保在使用完
TypedArray
后回收它。建议使用try finally块。
TypedValue val = new TypedValue();
boolean resolved = context.getTheme().resolveAttribute(R.attr.custom2, val, true);
if (resolved) {
    Log.i("Test", "Custom2 in theme");
} else {
    Log.i("Test", "Custom2 NOT in theme");
}
  private fun init(attrs: AttributeSet?) { 
    post {
        val attributes = context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.CustomButton,
            0, 0
        )
        .....
 private fun init(attrs: AttributeSet?) {
    val attributes = context.theme.obtainStyledAttributes(
        attrs,
        R.styleable.CustomButton,
        0, 0
    )
    post {
    ....