4 votes

Comment créer un bouton de forme personnalisée avec un sélecteur dans Android ?

MOCK UP


Button

Exigence


Je veux mettre un bouton personnalisé avec sélecteur .

La maquette est donnée ci-dessus.

Si quelqu'un connaît une solution, qu'il la partage.

Gracias.

13voto

Adrian Olar Points 1765

En gros, vous devez créer de nouveaux fichiers XML et les appliquer à votre élément Button. Comme je peux le voir sur la maquette, vous aurez besoin d'un trait et de la couleur d'arrière-plan avec un effet d'ombrage appliqué. Vous pouvez faire des recherches plus approfondies sur l'ombrage, mais la couleur d'arrière-plan et le trait sont assez simples.

Voici un exemple, done_rounded_btn.xml :

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:state_enabled="true" 
        android:drawable="@drawable/zzzzzzzzz_btn_orange" />
    <item 
        android:state_focused="true" 
        android:state_enabled="true"
        android:drawable="@drawable/zzzzzzzzz_btn_orange" />
    <item 
        android:state_focused="false" 
        android:state_enabled="false"
        android:drawable="@drawable/zzzzzzzzz_btn_inactiv" />
    <item android:drawable="@drawable/zzzzzzzzz_btn_black"/>
</selector>

pour la partie sélection et ensuite vous créez les drawables personnalisés correspondant à la maquette.

Un exemple, zzzzzzzzzz_btn_orange :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid
        android:color="@color/done_color">
    </solid>

    <corners
        android:bottomLeftRadius="3dp"
        android:bottomRightRadius="3dp"
        android:topLeftRadius="3dp"
        android:topRightRadius="3dp" />

</shape>

Et puis ajoutez-le à votre bouton comme arrière-plan, main.xml :

<Button
            android:id="@+id/registers_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/done_rounded_btn"
            android:text="@string/done_txt"
            android:textColor="@color/white"
            android:textSize="15sp" />

J'espère que cela vous aidera !

5voto

aniki.kvn Points 687

Vous pouvez l'utiliser à la place du bouton standard et définir le sélecteur comme arrière-plan dans le fichier XML :

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

/**
 * Custom Shape Button which ignores touches on transparent background.
 */
public class ButtonWithUntouchableTransparentBg extends Button {

    public ButtonWithUntouchableTransparentBg(Context context) {
        this(context, null);
    }

    public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setDrawingCacheEnabled(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        // ignores touches on transparent background
        if (isPixelTransparent(x, y))
            return true;
        else
            return super.onTouchEvent(event);
    }

    /**
     * @return true if pixel from (x,y) is transparent
     */
    private boolean isPixelTransparent(int x, int y) {
        Bitmap bmp = Bitmap.createBitmap(getDrawingCache());
        int color = Color.TRANSPARENT;
        try {
            color = bmp.getPixel(x, y);
        } catch (IllegalArgumentException e) {
            // x or y exceed the bitmap's bounds.
            // Reverts the View's internal state from a previously set "pressed" state.
            setPressed(false);
        }

        // Ignores touches on transparent background.
        if (color == Color.TRANSPARENT)
            return true;
        else
            return false;
    }
}

5voto

Till Points 1283

Vous pouvez également créer une forme qui utilise un sélecteur à l'intérieur. Si votre forme ne fait que changer de couleur dans différents états, cette méthode est beaucoup plus propre.

couleur/color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue_dark" android:state_pressed="true" />
    <item android:color="@color/blue_light" />
</selector>

dessinable/forme.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/color_selector" />
    <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
    <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>

0voto

Artemiy Points 1900

Bouton aux coins arrondis avec deux états (activé/désactivé) :

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <corners android:radius="28dp" />
            <solid android:color="@color/white" />
            <stroke android:width="1dp" android:color="@color/orange" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <corners android:radius="28dp" />
            <solid android:color="@color/grey_card_background" />
            <stroke android:width="1dp" android:color="@color/grey" />
        </shape>
    </item>
</selector>

-2voto

Muwaffaq imam Points 1

Dans votre élément, mettez la forme dans le sélecteur XML.

EX DE MON CODE :

<!-- if pressed -->
<item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle">
        <solid android:color="@color/blue" />

        <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
    </shape></item>

<!-- if not pressed -->
<item><shape android:padding="10dp" android:shape="rectangle">
        <solid android:color="@color/Purbble" />

        <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
    </shape></item>

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X