52 votes

Comment créer des boutons de boîte de dialogue dans le style du thème Holo d'Android ?

Je suis en train de créer une boîte de dialogue sur Holo Theme et je veux suivre la manière par défaut du système d'exploitation pour afficher les boutons. Jusqu'à présent, j'ai créé la boîte de dialogue mais les boutons ne s'affichent pas de la même manière que dans les applications réalisées en Holo pour ICS. Comment puis-je faire cela ? Le look & feel que je souhaite obtenir est le suivant No. 3rd in this image et je suis capable d'atteindre jusqu'ici Notice the Signup and Login buttons

85voto

SimonSays Points 6467

Un peu tard, mais peut-être que cela intéresse encore quelqu'un.

ça marche plutôt bien pour moi.

...
<!--
EDIT: be carefull, "?android:attr/dividerHorizontal" is only supported since API 11
      just avoid it in prior OSs.
-->
<View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerHorizontal" />
<LinearLayout 
    style="?android:attr/buttonBarStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="0dip"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:measureWithLargestChild="true">

    <Button 
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/cancel"/>
    <Button 
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/ok"/>
</LinearLayout>
...

l'activité qui charge cette mise en page a besoin du thème Holo.Dialog.

android:theme="@android:style/Theme.Holo.Dialog"

22voto

kishu27 Points 1979

C'est ce qui marche :

<LinearLayout
    android:id="@+id/buttonHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/cmdSignup"
        style="@android:style/Widget.Holo.Light.Button.Borderless.Small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Signup" />

    <Button
        android:id="@+id/cmdLogin"
        style="@android:style/Widget.Holo.Light.Button.Borderless.Small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Login" />
</LinearLayout>

La propriété style="@android:style/Widget.Holo.Light.Button.Borderless.Small" donne l'aspect plat et la sensation, et la distribution de poids de 50% est due à la combinaison de la taille de 100$ de LinearLayout par android:layout_width="match_parent" and Android:layout_weight="1"`pour les boutons

2voto

Lior Iluz Points 4348

Vous pouvez définir le thème par le biais du xml Manifest Android ou dans le onCreate de l'activité avec setTheme(android.R.style.Theme_Holo);

La taille des boutons n'est pas liée au thème lui-même. La taille dépend de vos définitions xml. Dans l'image que vous avez envoyée, il semble que les boutons aient reçu le thème Holo, donc il n'y a pas de problème ici...

Voici une mise en page xml qui étire les boutons pour qu'ils occupent toute la largeur du dialogue :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                >
                <Button
                    android:id="@+id/okButton"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="OK"
                />
                <Button
                    android:id="@+id/cancelButton"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Cancel"
                />          
        </LinearLayout>
</LinearLayout>

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