android设置渐变背景,Android LinearLayout渐变背景
我在將漸變背景應(yīng)用于LinearLayout時(shí)遇到問(wèn)題。
根據(jù)我所讀的內(nèi)容,這應(yīng)該相對(duì)簡(jiǎn)單,但似乎不起作用。 作為參考,我正在開(kāi)發(fā)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">
如果我將@ drawable / header_bg更改為一種顏色-例如 #FF0000它工作正常。 我在這里錯(cuò)過(guò)明顯的東西嗎?
android:backgroundTint android:backgroundTintMode stackoverflow.com/a/43341289/3209132
好的,我設(shè)法使用選擇器解決了這個(gè)問(wèn)題。參見(jiàn)下面的代碼:
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" />
希望這可以幫助遇到相同問(wèn)題的人。
大。 僅供參考,請(qǐng)參見(jiàn)其他漸變類(lèi)型:developer.android.com/reference/android/graphics/drawable/
也可以使用第三種顏色(中間)。以及各種形狀。
例如在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" />
這給了您黑色-灰色-黑色(從左到右),這是我最喜歡的深色背景atm。
記住要在您的布局xml中添加gradient.xml作為背景:
android:background="@drawable/gradient"
也可以通過(guò)以下方式旋轉(zhuǎn):
angle="0"
給你一條垂直線(xiàn)
與
angle="90"
給你一條水平線(xiàn)
可能的角度是:
0, 90, 180, 270.
也有幾種不同的形狀:
android:shape="rectangle"
圓形:
android:shape="oval"
可能還有更多。
希望能有所幫助,加油!
在XML Drawable文件中:
android:endColor="#9b0493"
android:startColor="#38068f"
android:type="linear" />
在您的布局文件中: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">
.....
嗨,您是如何實(shí)現(xiàn)透明狀態(tài)欄的? 如果我在styles.xml中將其設(shè)置為透明,它將變?yōu)楹谏?.
嘗試刪除android:gradientRadius =" 90"。這是一個(gè)對(duì)我有用的東西:
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
android:startColor="@color/purple"
android:endColor="@color/pink"
android:angle="270" />
不幸的是,這對(duì)我不起作用。 我已經(jīng)用現(xiàn)在的內(nèi)容更新了原始問(wèn)題。
在布局中添加小部件(例如TextView)時(shí),它仍然不起作用嗎?
正確-布局內(nèi)部的TextView仍然無(wú)法使用。 同樣,如果我應(yīng)用靜態(tài)顏色而不是可繪制對(duì)象,則效果很好。 我注意到的一件事是,我可以(有時(shí))使用選擇器來(lái)使其工作,但根據(jù)我的理解,這不是必需的。
我的問(wèn)題是.xml擴(kuò)展名未添加到新創(chuàng)建的XML文件的文件名中。添加.xml擴(kuò)展名解決了我的問(wèn)題。
您可以使用自定義視圖來(lái)執(zhí)行此操作。使用此解決方案,可以完成項(xiàng)目中所有顏色的漸變形狀:
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)
}
}
我還使用此自定義視圖創(chuàng)建了一個(gè)開(kāi)源項(xiàng)目GradientView:
https://github.com/lopspower/GradientView
implementation 'com.mikhaellopez:gradientview:1.1.0'
我不知道這是否對(duì)任何人都有用,但是我的問(wèn)題是我試圖將漸變?cè)O(shè)置為ImageView的" src"屬性,如下所示:
android:id="@+id/imgToast"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="@drawable/toast_bg"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
不能100%確認(rèn)為什么不起作用,但是現(xiàn)在我更改了它,并將drawable放在ImageView父對(duì)象的" background"屬性中,在我的情況下為RelativeLayout,如下所示:(此方法成功完成了)
android:layout_width="match_parent"
android:id="@+id/custom_toast_layout_id"
android:layout_height="match_parent"
android:background="@drawable/toast_bg">
我會(huì)在您的漸變顏色上檢查您的Alpha通道。對(duì)我來(lái)說(shuō),當(dāng)我測(cè)試代碼時(shí),我在顏色上設(shè)置了錯(cuò)誤的Alpha通道,它對(duì)我不起作用。一旦我設(shè)置了Alpha通道,它就可以正常工作!
總結(jié)
以上是生活随笔為你收集整理的android设置渐变背景,Android LinearLayout渐变背景的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CMakeList方法
- 下一篇: java excel导出(表头合并,多行