android设置渐变背景,Android LinearLayout渐变背景
我在將漸變背景應用于LinearLayout時遇到問題。
根據我所讀的內容,這應該相對簡單,但似乎不起作用。 作為參考,我正在開發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它工作正常。 我在這里錯過明顯的東西嗎?
android:backgroundTint android:backgroundTintMode stackoverflow.com/a/43341289/3209132
好的,我設法使用選擇器解決了這個問題。參見下面的代碼:
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" />
希望這可以幫助遇到相同問題的人。
大。 僅供參考,請參見其他漸變類型: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"
也可以通過以下方式旋轉:
angle="0"
給你一條垂直線
與
angle="90"
給你一條水平線
可能的角度是:
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">
.....
嗨,您是如何實現透明狀態欄的? 如果我在styles.xml中將其設置為透明,它將變為黑色..
嘗試刪除android:gradientRadius =" 90"。這是一個對我有用的東西:
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
android:startColor="@color/purple"
android:endColor="@color/pink"
android:angle="270" />
不幸的是,這對我不起作用。 我已經用現在的內容更新了原始問題。
在布局中添加小部件(例如TextView)時,它仍然不起作用嗎?
正確-布局內部的TextView仍然無法使用。 同樣,如果我應用靜態顏色而不是可繪制對象,則效果很好。 我注意到的一件事是,我可以(有時)使用選擇器來使其工作,但根據我的理解,這不是必需的。
我的問題是.xml擴展名未添加到新創建的XML文件的文件名中。添加.xml擴展名解決了我的問題。
您可以使用自定義視圖來執行此操作。使用此解決方案,可以完成項目中所有顏色的漸變形狀:
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)
}
}
我還使用此自定義視圖創建了一個開源項目GradientView:
https://github.com/lopspower/GradientView
implementation 'com.mikhaellopez:gradientview:1.1.0'
我不知道這是否對任何人都有用,但是我的問題是我試圖將漸變設置為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%確認為什么不起作用,但是現在我更改了它,并將drawable放在ImageView父對象的" background"屬性中,在我的情況下為RelativeLayout,如下所示:(此方法成功完成了)
android:layout_width="match_parent"
android:id="@+id/custom_toast_layout_id"
android:layout_height="match_parent"
android:background="@drawable/toast_bg">
我會在您的漸變顏色上檢查您的Alpha通道。對我來說,當我測試代碼時,我在顏色上設置了錯誤的Alpha通道,它對我不起作用。一旦我設置了Alpha通道,它就可以正常工作!
總結
以上是生活随笔為你收集整理的android设置渐变背景,Android LinearLayout渐变背景的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CMakeList方法
- 下一篇: Android NDK 之CmakeLi