可以用选择器绘制吗?
在可绘制文件夹中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/icon2" />
<item android:drawable="@drawable/icon3" />
</selector>
布局只是简单的线性布局,填充整个空间
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/on_touch_"
android:weightSum="1" android:focusable="true" android:focusableInTouchMode="true">
</LinearLayout>
,当我按下此按钮时,什么也不会发生
如果我添加,例如,一些文本视图并分配 android:background="@drawable/on_touch_" 然后该文本视图在按下时会正确更改图像。
线性布局的问题在哪里,为什么按下时不会改变图像?
编辑: 我确信我的可绘制选择器很好并且可以工作,因为我将其作为其他元素的背景并且它正在工作。
但我的问题是如何将可绘制对象设置为 root 元素
in drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/icon2" />
<item android:drawable="@drawable/icon3" />
</selector>
and the layout is just simple linear layout that fill the whole space
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/on_touch_"
android:weightSum="1" android:focusable="true" android:focusableInTouchMode="true">
</LinearLayout>
and when I press this nothing happens
If I add, for example, some textview and assign android:background="@drawable/on_touch_"
then that textview when pressed it changes the image correctly.
Where is the problem with the linear layout why it does not change the image when pressed ?
Edit:
I am sure that my drawable selector is good and working cause I put as a background to other elements and it is working.
But my problem is how to set the drawable to the root element
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其添加到您的布局中:
这将在您单击它时设置按下状态。
Add this to your layout :
This will set the pressed state when you'll click it.
尝试
save_selector.xml
并将布局的背景指定为选择器,例如
android:background="@drawable/save_selector
try out
save_selector.xml
and give layout's background as selector like
android:background="@drawable/save_selector
我会确保我没有一个可绘制的同名者。您似乎使用
on_touch_.xml
作为选择器。也许还有一个on_touch_.png
?我还要确保我不会在代码中再次将背景设置为其他内容,或者使其不可点击。
I would make sure I do not have a drawable namesake. You seem to be using
on_touch_.xml
as your selector. Is there perhaps also anon_touch_.png
?I would also make sure that I am not setting the background again, to something else, or making it unclickable, in code.
我在根元素中有一个工作正常的选择器;按下时背景切换。它不是活动布局的根元素,但它是 XML 文件的根元素。
背景首先分配有 png:
我仅在分配 OnClickListener 时将背景分配给选择器:
在我的 XML 中,我使用了
android:clickable="true"
但后来我还添加了android:focusable="true" android:focusableInTouchMode="true"
以匹配您的情况。这也很好用,可以切换我所有四个状态的背景。如果这对您不起作用,则说明您的代码在其他地方有其他问题。
当我添加或删除可绘制对象时,Eclipse 有时会变得可疑并将它们混合在一起。我的正常测量是:
I got a selector in a root element that is working fine; the background switches when pressed. It is not the root element of the activity layout, but it is the root element of the XML file.
The background is assigned with a png at first:
I assign the background to the selector only when assigning an OnClickListener:
In my XML, I used
android:clickable="true"
but then I also addedandroid:focusable="true" android:focusableInTouchMode="true"
to match your case. That also worked fine, with background switching for all my four states.If this is not working for you, something else is wrong with your code, somewhere else.
When I add or remove drawables, Eclipse sometimes gets fishy and mix them up. My normal measurements are: