2012-08-28 81 views
12

我不明白爲什麼這段代碼不起作用。誰能幫忙?我試圖讓按鈕使用名爲「greenstart」和「綠色」的自定義顏色。顏色已經在res/value/string.xml文件中創建。我看過類似的問題,但是他們沒有得到答覆,不清楚,或者解決方案不起作用。提前致謝。如何在android中通過xml添加漸變到按鈕?

XML文件示例:

<Button 
    android:id="@+id/mycollection" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" > 

    <Gradient 
     android:angle="270" 
     android:endColor="@color/greenstart" 
     android:startColor="@color/greenend" > 
    </Gradient> 
</Button> 

錯誤日誌:

08-28 21:47:20.574: E/AndroidRuntime(761): FATAL EXCEPTION: main 
08-28 21:47:20.574: E/AndroidRuntime(761): java.lang.RuntimeException: Unable to start activity ComponentInfo{My.Taste.App/My.Taste.App.MyTasteActivity}: android.view.InflateException: Binary XML file line #50: Error inflating class Gradient 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.os.Looper.loop(Looper.java:137) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.ActivityThread.main(ActivityThread.java:4424) 
08-28 21:47:20.574: E/AndroidRuntime(761): at java.lang.reflect.Method.invokeNative(Native Method) 
08-28 21:47:20.574: E/AndroidRuntime(761): at java.lang.reflect.Method.invoke(Method.java:511) 
08-28 21:47:20.574: E/AndroidRuntime(761): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
08-28 21:47:20.574: E/AndroidRuntime(761): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
08-28 21:47:20.574: E/AndroidRuntime(761): at dalvik.system.NativeStart.main(Native Method) 
08-28 21:47:20.574: E/AndroidRuntime(761): Caused by: android.view.InflateException: Binary XML file line #50: Error inflating class Gradient 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
08-28 21:47:20.574: E/AndroidRuntime(761): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.Activity.setContentView(Activity.java:1835) 
08-28 21:47:20.574: E/AndroidRuntime(761): at My.Taste.App.MyTasteActivity.onCreate(MyTasteActivity.java:34) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.Activity.performCreate(Activity.java:4465) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
08-28 21:47:20.574: E/AndroidRuntime(761): ... 11 more 
08-28 21:47:20.574: E/AndroidRuntime(761): Caused by: java.lang.ClassNotFoundException: android.view.Gradient 
08-28 21:47:20.574: E/AndroidRuntime(761): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
08-28 21:47:20.574: E/AndroidRuntime(761): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
08-28 21:47:20.574: E/AndroidRuntime(761): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.createView(LayoutInflater.java:552) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636) 
08-28 21:47:20.574: E/AndroidRuntime(761): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653) 
08-28 21:47:20.574: E/AndroidRuntime(761): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678) 
08-28 21:47:20.574: E/AndroidRuntime(761): ... 23 more 

回答

49

創建一個新的XML文件,並把它放在繪製,然後將其添加到按鈕背景

gradient.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
    <!-- Gradient Bg for listrow --> 
    <gradient 
     android:startColor="#f1f1f2" 
     android:centerColor="#e7e7e8" 
     android:endColor="#cfcfcf" 
     android:angle="270" /> 
</shape> 

layout.xml

<Button 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:background="@drawable/gradient" 
    android:text="Übernehmen" > 
+0

會怎麼做我把它作爲按鈕化背景? 「android:background =」@?「 – jrquick

+0

button code is already added。看到android:背景 –

+0

完美答案!謝謝! – gts101

9

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
     <shape> 
      <solid 
       android:color="#70c656" /> 
      <stroke 
       android:width="1dp" 
       android:color="#53933f" /> 
      <corners 
       android:radius="3dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 
    <item> 
     <shape> 
      <gradient 
       android:startColor="#70c656" 
       android:endColor="#53933f" 
       android:angle="270" /> 
      <stroke 
       android:width="1dp" 
       android:color="#53933f" /> 
      <corners 
       android:radius="4dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 
</selector> 
4

創建gradient.xml

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

    <gradient 
     android:angle="270" 
     android:endColor="@color/greenstart" 
     android:startColor="@color/greenend" /> 

</shape> 
0

所以在這裏我們隨着漸變走。如上所述@Dusean Singh說。如果您將使用角度270那麼你的梯度將開始從上往下:TOP - >中心 - >底部

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#FF0000" 
     android:centerColor="#00FF00" 
     android:endColor="#0000FF" 
     android:angle="270" /> 
</shape> 

enter image description here

如果要使用的角度3600那麼你的漸變將從頂部開始向下:左 - >中心 - >右鍵

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <!-- Gradient Bg for listrow --> 
    <gradient 
     android:startColor="#FF0000" 
     android:centerColor="#00FF00" 
     android:endColor="#0000FF" 
     android:angle="360" /> 
</shape> 

enter image description here

在這裏,我們去的效果。以及如何應用相同的按鈕

<LinearLayout 
    android:id="@+id/design_bottom_sheet" 
    android:layout_alignParentBottom="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2"> 
    <Button 
     android:drawableLeft="@drawable/ic_grid" 
     android:layout_width="match_parent" 
     android:text="Find" 
     android:background="@drawable/gradient_button" 
     android:textColor="@color/white" 
     android:textAllCaps="false" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <Button 
     android:drawableLeft="@drawable/ic_card" 
     android:layout_width="match_parent" 
     android:textColor="@color/white" 
     android:text="Match" 
     android:background="@drawable/gradient_button" 
     android:textAllCaps="false" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 

enter image description here

相關問題