2016-10-10 34 views
0

在我的android应用程序中,我想让我的按钮变成绿色和圆角矩形,当有人按下按钮时,它应该将颜色更改为灰色。所以我创建了一个带有选择器的XML文件,并将其赋予圆角矩形的形状,但问题在于当我启动我的应用程序时,默认情况下颜色不是绿色,而是透明。此外,当我按下按钮时,它显示矩形形状,所以我认为shapre也不起作用。这是我的主要颜色:形状和选择器不工作

<color name="colorPrimary">#669900</color> 

我custom_button.xml

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 
     <item android:state_focused="true" android:state_pressed="false" android:color="@color/colorPrimary" android:drawable="@color/colorPrimary"> 
     <shape 
      android:shape="rectangle" android:padding="10dp"> 
      <solid android:color="@color/colorPrimary"/> 
      <corners 
       android:bottomRightRadius="15dp" 
       android:bottomLeftRadius="15dp" 
       android:topLeftRadius="15dp" 
       android:topRightRadius="15dp"/> 
     </shape> 
    </item> 
     <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" > 
      <shape 
       android:shape="rectangle" android:padding="10dp"> 
       <corners 
        android:bottomRightRadius="15dp" 
        android:bottomLeftRadius="15dp" 
        android:topLeftRadius="15dp" 
        android:topRightRadius="15dp"/> 
      </shape> 
     </item> 
     <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" > 
      <shape 
       android:shape="rectangle" android:padding="10dp"> 
       <corners 
        android:bottomRightRadius="15dp" 
        android:bottomLeftRadius="15dp" 
        android:topLeftRadius="15dp" 
        android:topRightRadius="15dp"/> 
      </shape> 
     </item> 
    </selector> 

而且我已经做了XML文件梯度是gradient.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <gradient android:angle="90" 
       android:startColor="#880f0f10" 
       android:centerColor="#880d0d0f" 
       android:endColor="#885d5d5e"/> 
     </shape> 
    </item> 
</layer-list> 

回答

0

要进入四舍五入的矩形,你可以试试下面的代码:

<?xml version="1.0" encoding="utf-8"?> 
    <layer-list 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item> 
      <shape 
       android:shape="rectangle"> 
       <corners 
        android:radius="5dp"/> 
       <stroke 
        android:width="2dp" 
        android:color="@color/border_of_rectangle"/> 
       <solid 
        android:color="@color/color_inside_rectangle"/> 
      </shape> 
     </item> 
    </layer-list> 
0

我做了什么类似之前这里是我怎么做

button.xml

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

button_default.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners 
     android:radius="@dimen/radius" /> 
    <solid 
     android:color="?attr/colorAccent" /> 
    <padding 
     android:bottom="10dp" 
     android:left="10dp" 
     android:right="10dp" 
     android:top="10dp" 
     /> 
</shape> 

button_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners 
     android:radius="@dimen/radius" /> 
    <solid 
     android:color="?attr/colorAccent" /> 
    <padding 
     android:bottom="10dp" 
     android:left="10dp" 
     android:right="10dp" 
     android:top="10dp" 
     /> 
</shape> 

值/ styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 

     <item name="android:buttonStyle">@style/BlueButton</item> 
    </style> 

    <style name="BlueButton" parent="android:style/Widget.Button"> 
     <item name="android:background">@drawable/button</item> 
     <item name="android:textColor">@drawable/button_text_color</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textSize">18sp</item> 
    </style> 

</resources> 

我也猜你可能需要改变文字颜色,所以这里是文件 button_text_color .XML

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="?attr/colorPrimaryDark" android:state_pressed="false" android:state_focused="false"/> 
    <item android:color="?attr/colorAccent" android:state_pressed="true"/> 
    <item android:color="?attr/colorAccent" android:state_focused="true" /> 
</selector>