可以用选择器绘制吗?

发布于 2024-12-09 12:58:55 字数 951 浏览 1 评论 0原文

在可绘制文件夹中:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗失的美好 2024-12-16 12:58:55

将其添加到您的布局中:

 android:clickable="true"

这将在您单击它时设置按下状态。

Add this to your layout :

 android:clickable="true"

This will set the pressed state when you'll click it.

痞味浪人 2024-12-16 12:58:55

尝试

save_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">



    <item android:state_focused="true" android:state_pressed="false"
        android:drawable="@drawable/save_hover_new" />
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/save_hover_new" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/save_hover_new" />

    <item android:drawable="@drawable/save_new" />

</selector>

并将布局的背景指定为选择器,例如
android:background="@drawable/save_selector

try out

save_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">



    <item android:state_focused="true" android:state_pressed="false"
        android:drawable="@drawable/save_hover_new" />
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/save_hover_new" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/save_hover_new" />

    <item android:drawable="@drawable/save_new" />

</selector>

and give layout's background as selector like
android:background="@drawable/save_selector

拥有 2024-12-16 12:58:55

我会确保我没有一个可绘制的同名者。您似乎使用 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 an on_touch_.png?

I would also make sure that I am not setting the background again, to something else, or making it unclickable, in code.

凉栀 2024-12-16 12:58:55

我在根元素中有一个工作正常的选择器;按下时背景切换。它不是活动布局的根元素,但它是 XML 文件的根元素。

背景首先分配有 png:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/panel"
>
    ...

我仅在分配 OnClickListener 时将背景分配给选择器:

private void setClickListener(OnClickListener ocl) {

    View boxRoot = findViewById(R.id.box_root);
    if (ocl != null) {
        boxRoot.setOnClickListener(ocl);
        boxRoot.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_arrow_right_bgstate));

        setClickable(true); 
                    ...

在我的 XML 中,我使用了 android:clickable="true" 但后来我还添加了 android:focusable="true" android:focusableInTouchMode="true" 以匹配您的情况。这也很好用,可以切换我所有四个状态的背景。

// panel_arrow_right_bgstate.xml
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/panel_arrow_right_normal"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/panel_arrow_right_pressed"/>
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/panel_arrow_right_selected"/>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/panel_arrow_right_pressed"/>

如果这对您不起作用,则说明您的代码在其他地方有其他问题。

当我添加或删除可绘制对象时,Eclipse 有时会变得可疑并将它们混合在一起。我的正常测量是:

  1. 清理项目
  2. 刷新 Eclipse 中的 res 文件夹
  3. 删除 gen\com.comp.proj\R.java

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:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/panel"
>
    ...

I assign the background to the selector only when assigning an OnClickListener:

private void setClickListener(OnClickListener ocl) {

    View boxRoot = findViewById(R.id.box_root);
    if (ocl != null) {
        boxRoot.setOnClickListener(ocl);
        boxRoot.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_arrow_right_bgstate));

        setClickable(true); 
                    ...

In my XML, I used android:clickable="true" but then I also added android:focusable="true" android:focusableInTouchMode="true" to match your case. That also worked fine, with background switching for all my four states.

// panel_arrow_right_bgstate.xml
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/panel_arrow_right_normal"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/panel_arrow_right_pressed"/>
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/panel_arrow_right_selected"/>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/panel_arrow_right_pressed"/>

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:

  1. Clean project
  2. Refresh res folder in Eclipse
  3. Delete gen\com.comp.proj\R.java
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文