本文介绍了?如何"机器人:ATTR / activatedBackgroundIndicator"工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何显示供选择上下文操作栏时,突出显示列表中选定的项目,我找到了解决办法是将安卓背景我行布局XML来属性。安卓?ATTR / activatedBackgroundIndicator

如何设定这项工作有关系吗?

  1. 什么是涉及的机制?
  2. 在做什么样的语法元素?,ATTR,activatedBackgroundIndicator是什么意思?
  3. 在哪里是activatedBackgroundIndicator的含义界定?
解决方案

如果你是在一个法医的心情在这里是如何挖掘并找出到底是怎么回事。

 安卓背景=机器人:ATTR / activatedBackgroundIndicator?
 

直观地看这意味着将背景设置为一些绘制。

但是让我们进一步分解这个,看看我们如何让我们的神秘的绘制。

要为precise它的意思是设置背景属性,哪些属性activatedBackgroundIndicator指的是当前主题

如果你理解,是指在当前主题的一部分,你基本上已经明白了一切,是要在后面的盖子。

基本上, activatedBackgroundIndicator不是实际的绘制,但引用了一个绘制。那么,是activateBackgroundIndictor属性实际上定义的?

它在一个文件名你的SDK目录中定义的 attrs.xml 。例如:

如果你打开​​这个文件,你会声明如下:

 < attr指示NAME =activatedBackgroundIndicator格式=参考/>
 

attrs.xml是你声明的所有属性,你是以后要在您的视图的XML使用。 请注意,我们在声明属性,它的类型,而不是实际分配值这里

实际值被分配在的themes.xml 。该文件位于:

如果你打开​​这个文件,你会看到多重定义取决于什么主题,你正在使用。例如,这里有主题名称主题定义,Theme.Light,Theme.Holo,Theme.Holo.Light分别为:

 <项目名称=activatedBackgroundIndicator> @android:绘制/ activated_background< /项目>
<项目名称=activatedBackgroundIndicator> @android:绘制/ activated_background_light< /项目>
<项目名称=activatedBackgroundIndicator> @android:绘制/ activated_background_holo_dark< /项目>
&LT;项目name="activatedBackgroundIndicator">@android:drawable/activated_background_holo_light</item>
 

现在,我们有我们的神秘的可绘制。如果你选择第一个,它在定义在绘制文件夹:

如果您打开一个文件,你会看到哪些重要的是要了解正在发生的事情可绘的定义。

 &LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目安卓state_activated =真正的机器人:可绘制=@机器人:可绘制/ list_selector_background_selected/&GT;
    &LT;项目机器人:可绘制=@色/透明/&GT;
&LT; /选择器&GT;
 

在这里我们定义了一个绘制有两种状态 - 默认状态就是透明的背景,如果状态为state_activated那么我们绘制的list_selector_background_selected。

关于在可绘区域和国家的背景信息链接

list_selector_background_selected是位于绘制,华电国际文件夹中的9补丁PNG文件。

现在你可以看到为什么我们定义activatedBackgroundIndicator作为参考,而不是直接链接到可绘文件 - 它可以让你选择正确的绘制根据你的主题

I was looking for how to highlight a selected item in a list when displaying a contextual action bar for the selection, and the solution I found was to set the android:background attribute of my row layout xml to "?android:attr/activatedBackgroundIndicator".

How does setting this work though?

  1. what is the mechanism involved?
  2. what do the syntax elements like "?", "attr", "activatedBackgroundIndicator" mean?
  3. where is the meaning of "activatedBackgroundIndicator" defined?
解决方案

If you are in a forensic mood here is how to dig and find out what is going on.

android:background="?android:attr/activatedBackgroundIndicator"?

Intuitively this means set the background to some drawable.

But lets decompose this further to see how we get to our mysterious drawable.

To be precise it means "set the background attribute to what the attribute "activatedBackgroundIndicator" refers to in the current theme.

If you understand "refers to in the current theme" part, you have basically understood everything that is going on behind the covers.

Basically, activatedBackgroundIndicator is not an actual drawable but a reference to a drawable. So where is "activateBackgroundIndictor" attribute actually defined?

Its defined in your sdk directory in a file name attrs.xml. For example:

If you open that file, you will the declaration as follows:

<attr name="activatedBackgroundIndicator" format="reference" />

attrs.xml is where you declare all the attributes that you are later going to use in your view xml. Note we are declaring the attribute and its type and not actually assigning a value here.

The actual value is assigned in themes.xml. This file is located at:

If you open that file, you will see the multiple definitions depending on what theme you are using. For example, here are the definitions for themes name Theme, Theme.Light, Theme.Holo, Theme.Holo.Light respectively:

<item name="activatedBackgroundIndicator">@android:drawable/activated_background</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background_light</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background_holo_dark</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background_holo_light</item>

Now we have our mysterious drawables. If you pick the first one, it is defined in the drawable folder at:

If you open that file you will see the definition of the drawable which is important to understanding what is going on.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@android:drawable/list_selector_background_selected" />
    <item android:drawable="@color/transparent" />
</selector>

Here we are defining a drawable with two states - default state is just transparent background and if the state is "state_activated" then our drawable is "list_selector_background_selected".

see this link for background information on on drawables and states.

"list_selector_background_selected" is a 9 patch png file that is located in the drawable-hdpi folder.

Now you can see why we defined activatedBackgroundIndicator as a reference rather than linking directly to the drawable file - it allows you to pick the right drawable depending on your theme.

这篇关于?如何&QUOT;机器人:ATTR / activatedBackgroundIndicator&QUOT;工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:15