Android LinearLayout Gradient Background

前端 未结 10 1778
渐次进展
渐次进展 2020-12-02 04:20

I am having trouble applying a gradient background to a LinearLayout.

This should be relatively simple from what I have read but it just doesn\'t seem to work. For r

相关标签:
10条回答
  • 2020-12-02 04:41

    You can used a custom view to do that. With this solution, it's finished the gradient shapes of all colors in your projects:

    class GradientView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    
        // Properties
        private val paint: Paint = Paint()
        private val rect = Rect()
    
        //region Attributes
        var start: Int = Color.WHITE
        var end: Int = Color.WHITE
        //endregion
    
        override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
            super.onSizeChanged(w, h, oldw, oldh)
            // Update Size
            val usableWidth = width - (paddingLeft + paddingRight)
            val usableHeight = height - (paddingTop + paddingBottom)
            rect.right = usableWidth
            rect.bottom = usableHeight
            // Update Color
            paint.shader = LinearGradient(0f, 0f, width.toFloat(), 0f,
                    start, end, Shader.TileMode.CLAMP)
            // ReDraw
            invalidate()
        }
    
        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            canvas.drawRect(rect, paint)
        }
    
    }
    

    I also create an open source project GradientView with this custom view:

    https://github.com/lopspower/GradientView

    implementation 'com.mikhaellopez:gradientview:1.1.0'
    
    0 讨论(0)
  • 2020-12-02 04:48

    It is also possible to have the third color (center). And different kinds of shapes.

    For example in drawable/gradient.xml:

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="#000000"
            android:centerColor="#5b5b5b"
            android:endColor="#000000"
            android:angle="0" />
    </shape>
    

    This gives you black - gray - black (left to right) which is my favorite dark background atm.

    Remember to add gradient.xml as background in your layout xml:

    android:background="@drawable/gradient"
    

    It is also possible to rotate, with:

    angle="0"

    gives you a vertical line

    and with

    angle="90"

    gives you a horizontal line

    Possible angles are:

    0, 90, 180, 270.

    Also there are few different kind of shapes:

    android:shape="rectangle"

    Rounded shape:

    android:shape="oval"

    and problably a few more.

    Hope it helps, cheers!

    0 讨论(0)
  • 2020-12-02 04:49

    I don't know if this will help anybody, but my problem was I was trying to set the gradient to the "src" property of an ImageView like so:

    <ImageView 
        android:id="@+id/imgToast"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:src="@drawable/toast_bg"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"/>
    

    Not 100% sure why that didn't work, but now I changed it and put the drawable in the "background" property of the ImageView's parent, which is a RelativeLayout in my case, like so: (this worked successfully)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/custom_toast_layout_id"
        android:layout_height="match_parent"
        android:background="@drawable/toast_bg">
    
    0 讨论(0)
  • 2020-12-02 04:51

    I would check your alpha channel on your gradient colors. For me, when I was testing my code out I had the alpha channel set wrong on the colors and it did not work for me. Once I got the alpha channel set it all worked!

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