How to make a gradient background for a LinearLayout?

前端 未结 2 953
我寻月下人不归
我寻月下人不归 2021-02-09 16:19

I was wondering, what would be the best way to make a gradient background for a LinearLayout in java ( not xml ) ?

Any ideas?

Thanks!

相关标签:
2条回答
  • 2021-02-09 16:41

    Besides xml you can also use GradientDrawable it has corresponding methods for all xml attributes

    0 讨论(0)
  • 2021-02-09 16:42
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="#006499"
              android:endColor="#0093d7"
            android:angle="90" />
    </shape>
    

    Set here startColor and endColor as your requirement and save this file in drawable folder

    and in LinearLayout you can set this as setBackground="@drawable/your gradient filename"

    Using java code you can do same thing using GradientDrawable

    0 讨论(0)
提交回复
热议问题