2013-10-31 21 views
5

Tôi có ba nút đơn giản trong chế độ xem hoạt động chính của mình. Những gì tôi đang cố gắng làm là áp dụng một kiểu nút cho tất cả các nút của tôi nhưng tôi không làm như vậy.Kiểu chủ đề Android cho nút không áp dụng

Dưới đây là xml của tôi:

<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> 

Và đây là style.xml của tôi từ res/values ​​thư mục:

<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> 

Và định nghĩa chủ đề của tôi từ file manifest:

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

Bất kỳ ý tưởng nào tại sao kiểu không áp dụng cho các nút của tôi? Tôi đã đặt màu văn bản của một nút thành màu trắng chỉ để thấy bất kỳ sự khác biệt nào.

+0

Có thể hoạt động của bạn sẽ ghi đè theme ứng dụng. – Bracadabra

Trả lời

33

Khi sử dụng các thư viện hỗ trợ các phong cách nút phải được áp dụng như:

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

Lưu ý trùng lặp nhưng không có không gian tên

+0

Cảm ơn. Đây phải là câu trả lời được chấp nhận. –

+0

Nó hoạt động! Hãy Robbie, chấp nhận câu trả lời này rất hữu ích. – TechNyquist

+0

Với thư viện hỗ trợ, chỉ cần mục không có tên được yêu cầu để thực hiện tác phẩm này. Không cần sao chép – yuval

3

Mã mà bạn đăng đang hoạt động. Vui lòng thêm phong cách trên nút trong xml, để xem nếu đó là làm việc cho bạn.

<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

Không kiểu nào không áp dụng theo cách đó – DerpyNerd

+1

Xóa android: background = "@ drawable/button_selector" khỏi nút và thử lại. Tôi không chắc tại sao điều này không hiệu quả. Và thử nó trên giả lập cũng. – Dragan

0

Thử chạy "sạch" trên dự án khi thay đổi kiểu hoặc tệp kê khai.

Các vấn đề liên quan