Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

image - Android ImageButton with disabled UI feel

I have an ImageButton which is disabled (non clickable or set as disabled). I want to give an UI feel to the user that it is disabled without using any other image.

Is there any way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unlike a regular Button, an ImageButton or a Button that has an image background is not grayed when disabled. You actually have to use another image or to process it in a way it appears grayed.

Should using another image be ok, you can do this by using a <selector> (here associated to a regular Button but this amongs to the same):

  • /drawable/my_selector.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false"
            android:drawable="@drawable/button_gray" /> ***button_gray is a Drawable image***
        <item android:state_pressed="true"
            android:drawable="@drawable/button_gray" /> 
        <item android:drawable="@drawable/button_red" /> ***button_red is a Drawable image*** 
    </selector>
    

Please note that in a selector the logic applies a sequential way, item per item. Here, button_red is used all the time but when the button is disabled or being pushed.

  • Your layout.xml:

    <Button android:id="@+id/myButton"
            android:background="@drawable/my_selector" ***this is a reference to the selector above ***
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    />
    

And should using another image be a problem, other answers (such as @Tronman's or @southerton's) give you ways to programmatically process the image in a way it appears grayed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...