2016-05-15 17 views
0

おはよう。ボタンのスタイルを変更してボタンの色を変更する

アクセントの色にしたくないのでボタンの色を変えてみました。したがって、私はボタンのスタイルを変更しようとしました。以下は私のコードです:

<android.support.v7.widget.AppCompatButton 
    android:id="@+id/enterButton" 
    android:layout_width="200dp" 
    android:layout_height="48dp" 
    android:layout_marginTop="50dp" 
    android:layout_gravity="center" 
    android:text="@string/enter_button" 
    android:textSize="@dimen/sohere_font_small" 
    style="@style/NewButtonStyle" 
    android:enabled="true"/> 

    <style name="NewButtonStyle" parent="Widget.AppCompat.Button.Colored"> 
    <item name="colorButtonNormal">@drawable/selector_button_color</item> 
    <item name="android:textColor">@drawable/selector_button_text_color</item> 
    <item name="colorAccent">@color/primary_color</item> 
</style> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="true" 
     android:color="@color/primary_color" /> 
    <item 
     android:state_enabled="false" 
     android:color="@color/default_disabled_button_colour" /> 
</selector> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="true" 
     android:color="@color/white_color" /> 
    <item 
     android:state_enabled="false" 
     android:color="@color/default_hint_text_color" /> 
</selector> 

私は自分のコードを修正するまで、ボタンの色が変わりませんが:

<android.support.v7.widget.AppCompatButton 
    android:id="@+id/enterButton" 
    android:layout_width="200dp" 
    android:layout_height="48dp" 
    android:layout_marginTop="50dp" 
    android:layout_gravity="center" 
    android:text="@string/enter_button" 
    android:textSize="@dimen/sohere_font_small" 
    **android:theme="@style/NewButtonStyle"** 
    android:enabled="true"/> 

私は変更ボタンのスタイルを設定することにより、ボタンの色を変えることができる方法はありますテーマ?

アドバイスをしてください。以下が含まれている(例えばbutton.xml)ファイルと呼ばれるスタイルを描画可能なフォルダに

行くとxmlを行います:

答えて

0

あなたのボタンの装飾のためには、これを使用することができ

<?xml version="1.0" encoding="utf-8"?> 

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

    <item> 
     <shape> 
      <gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" /> 
      <stroke android:width="1px"   android:color="#000000" /> 
       <corners android:bottomLeftRadius="0dp" 
       android:bottomRightRadius="0dp" 
       android:topLeftRadius="8dp" 
       android:topRightRadius="8dp"/>  
       <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> 
     </shape> 
    </item> 

</selector> 

は、これは私のコードあなたが

をしたい、必要な変更は今、この

android:background="@drawable/button.xml" 
のようなあなたのレイアウトXML(mainactivity.xml)でそれを呼び出すことができます今3210

あなたは

<style name="buttonStyle" parent="@android:style/Widget.Button.Small"> 
    <item name="android:textColor">#FFFFFF</item> 
    <item name="android:textSize">12sp</item> 
    <item name="android:textStyle">bold</item> 
</style> 

さて、このようにレイアウトXML(mainactivity.xml)でこれを呼び出して値フォルダ内のstyles.xmlの一部として提供されます以下を使用することができますフォントの色やスタイルを変更するには

style="@style/buttonStyle" 

最終的なコードは次のとおりです。

<Button 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="15dp" 
android:text="@string/edit" 
android:id="@+id/btnEdit" 
android:background="@drawable/button.xml" 
style="@style/buttonStyle" 
/> 

・ホープ、このことができます:)

関連する問題