日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android layout background,Android LinearLayout Gradient Background

發(fā)布時(shí)間:2024/10/8 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android layout background,Android LinearLayout Gradient Background 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題

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 reference sakes I am developing on 2.1-update1.

header_bg.xml:

android:shape="rectangle">

android:angle="90"

android:startColor="#FFFF0000"

android:endColor="#FF00FF00"

android:type="linear"/>

main_header.xml:

android:layout_width="fill_parent"

android:layout_height="50dip"

android:orientation="horizontal"

android:background="@drawable/header_bg">

If I change @drawable/header_bg to a color - e.g. #FF0000 it works perfectly fine. Am I missing something obvious here?

回答1:

Ok I have managed to solve this using a selector. See code below:

main_header.xml:

android:layout_width="fill_parent"

android:layout_height="50dip"

android:orientation="horizontal"

android:background="@drawable/main_header_selector">

main_header_selector.xml:

android:angle="90"

android:startColor="#FFFF0000"

android:endColor="#FF00FF00"

android:type="linear" />

Hopefully this helps someone who has the same problem.

回答2:

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

For example in drawable/gradient.xml:

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

android:shape="rectangle">

android:startColor="#000000"

android:centerColor="#5b5b5b"

android:endColor="#000000"

android:angle="0" />

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!

回答3:

In XML Drawable File:

android:endColor="#9b0493"

android:startColor="#38068f"

android:type="linear" />

In your layout file: android:background="@drawable/gradient_background"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/gradient_background"

android:orientation="vertical"

android:padding="20dp">

.....

回答4:

Try removing android:gradientRadius="90". Here is one that works for me:

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

android:shape="rectangle"

>

android:startColor="@color/purple"

android:endColor="@color/pink"

android:angle="270" />

回答5:

My problem was the .xml extension was not added to the filename of the newly created XML file. Adding the .xml extension fixed my problem.

回答6:

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:

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)

android:layout_width="match_parent"

android:id="@+id/custom_toast_layout_id"

android:layout_height="match_parent"

android:background="@drawable/toast_bg">

回答7:

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!

回答8:

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'

來源:https://stackoverflow.com/questions/5976805/android-linearlayout-gradient-background

總結(jié)

以上是生活随笔為你收集整理的android layout background,Android LinearLayout Gradient Background的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。