2013-10-31 15 views
5

Ich habe drei einfache Schaltflächen in meiner Hauptansicht Aktivität. Was ich versuche, ist, einen Buttonstyle auf alle meine Buttons anzuwenden, aber ich versage es.Android-Design-Stil für Schaltfläche nicht anwenden

Hier ist meine xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/minecraft_portrait" 
    android:alpha="0.75" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/txtAppName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/app_name_readable" 
     android:textSize="32sp" 
     android:textColor="@android:color/white" /> 

    <Button 
     android:id="@+id/btnCrafting" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/button_selector" 
     android:onClick="craftingButtonClicked" 
     android:text="@string/crafting" /> 

    <Button 
     android:id="@+id/btnServerCommands" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/btnCrafting" 
     android:layout_below="@id/btnCrafting" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/button_selector" 
     android:onClick="commandsButtonClicked" 
     android:textColor="@android:color/white" 
     android:text="@string/servercommands" /> 

    <Button 
     android:id="@+id/btnVideos" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/btnServerCommands" 
     android:layout_below="@id/btnServerCommands" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/button_selector" 
     android:onClick="videosButtonClicked" 
     android:text="@string/videos" /> 

</RelativeLayout> 

Und das ist mein style.xml aus der res/Wert Ordner:

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

    <style name="AppBaseTheme" parent="android:Theme.Light"> 
    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <item name="android:buttonStyle">@style/ButtonTheme</item> 
    </style> 

    <style name="ButtonTheme" parent="android:Widget.Button"> 
     <item name="android:textColor">@android:color/white</item> 
     <item name="android:background">@android:color/darker_gray</item> 
     <item name="android:typeface">monospace</item> 
     <item name="android:textSize">32sp</item> 
    </style> 
</resources> 

Und mein Thema Definition aus der Manifest-Datei:

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
.... 

Irgendwelche Ideen, warum der Stil nicht auf meine Knöpfe zutrifft? Ich habe die Textfarbe einer Taste auf weiß eingestellt, um einen Unterschied zu sehen.

+0

Kann Ihre Aktivität überschreibt App Thema sein. – Bracadabra

Antwort

33

Wenn die Support-Bibliothek mit Hilfe der Schaltfläche Stil angewendet werden muss:

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 
    <item name="android:buttonStyle">@style/ButtonTheme</item> 
    <item name="buttonStyle">@style/ButtonTheme</item> 
</style> 

Notiere die Vervielfältigung aber ohne Namensraum

+0

Danke. Dies sollte eine akzeptierte Antwort sein. –

+0

Es hat funktioniert! Bitte Robbie, akzeptiere diese Antwort, die sehr hilfreich ist. – TechNyquist

+0

Mit der Support-Bibliothek ist nur der nicht namespaced Artikel erforderlich, um das zu funktionieren. Es gibt keine Notwendigkeit für die Duplizierung – yuval

3

Der von Ihnen gepostete Code funktioniert. Bitte fügen Sie einen Stil für die Schaltfläche in XML hinzu, um zu sehen, ob das für Sie funktioniert.

<Button 
    style="@style/ButtonTheme" 

    android:id="@+id/btnCrafting" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="@drawable/button_selector" 
    android:onClick="craftingButtonClicked" 
    android:text="@string/crafting" /> 
+1

Nein, der Stil passt nicht so – DerpyNerd

+1

Entfernen Sie Android: background = "@ Drawable/button_selector" von der Schaltfläche und versuchen Sie es erneut. Ich bin mir nicht sicher, warum das nicht funktioniert. Und versuche es auch auf dem Emulator. – Dragan

0

Versuchen Sie "sauber" auf dem Projekt auszuführen, wenn Sie Stile oder die Manifestdatei ändern.

Verwandte Themen