Android 선형 레이아웃 그라데이션 배경
선형 레이아웃에 그라데이션 배경을 적용하는 데 문제가 있습니다.
제가 읽은 바로는 이것은 비교적 간단해야 하지만 효과가 없는 것 같습니다.참고로 2.1-update1에서 개발 중입니다.
header_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear"/>
</shape>
main_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/header_bg">
</LinearLayout>
@drawable/header_bg를 #FF0000과 같은 색상으로 변경하면 완벽하게 작동합니다.제가 여기서 뭔가 명백한 것을 놓치고 있나요?
네, 선택기를 사용하여 이 문제를 해결했습니다.아래 코드 참조:
main_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/main_header_selector">
</LinearLayout>
main_main_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
이것이 같은 문제를 가진 사람에게 도움이 되기를 바랍니다.
세 번째 색상(가운데)을 가질 수도 있습니다.그리고 다른 종류의 모양들.
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>
검은색 - 회색 - 검은색(왼쪽에서 오른쪽)을 사용할 수 있으며, 이는 내가 가장 좋아하는 어두운 배경 분위기입니다.
레이아웃 xml에서 gradient.xml을 배경으로 추가해야 합니다.
android:background="@drawable/gradient"
다음과 같은 방법으로 회전할 수도 있습니다.
각도="0"
수직선을 제공합니다.
와 함께
각도="90"
수평 라인을 제공합니다.
가능한 각도는 다음과 같습니다.
0, 90, 180, 270.
또한 몇 가지 다른 종류의 모양이 있습니다.
Android:shape="message"
둥근 모양:
Android:shape="message"
그리고 아마 몇 개 더 있을 겁니다.
XML 그리기 가능 파일:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="90"
android:endColor="#9b0493"
android:startColor="#38068f"
android:type="linear" />
</shape>
</item>
</selector>
레이아웃 파일: Android:background="@drawable/drawable_background"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
android:orientation="vertical"
android:padding="20dp">
.....
</LinearLayout>
Android:gradius="90"을 제거해 보십시오.다음은 저에게 적합한 솔루션입니다.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="@color/purple"
android:endColor="@color/pink"
android:angle="270" />
</shape>
Kotlin을 사용하면 단 두 줄로 그것을 할 수 있습니다.
배열에서 색상 값 변경
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(Color.parseColor("#008000"),
Color.parseColor("#ADFF2F"))
);
gradientDrawable.cornerRadius = 0f;
//Set Gradient
linearLayout.setBackground(gradientDrawable);
결과
문제는 새로 생성된 XML 파일의 파일 이름에 .xml 확장자가 추가되지 않았다는 것입니다..xml 확장자를 추가하여 문제를 해결했습니다.
당신의 그라데이션 색상에 대한 알파 채널을 확인하겠습니다.저는 코드를 테스트할 때 알파 채널이 색상으로 잘못 설정되어 있어서 작동하지 않았습니다.알파 채널 세트를 얻자 모든 것이 작동했습니다!
이것이 누군가에게 도움이 될지는 모르겠지만, 제 문제는 다음과 같이 ImageView의 "src" 속성에 그라데이션을 설정하려고 한다는 것이었습니다.
<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"/>
왜 그것이 작동하지 않았는지 100% 확신할 수는 없지만, 이제 저는 그것을 변경하고 ImageView 부모의 "배경" 속성에 그리기 가능한 그림을 넣었습니다. 이 경우에는 상대 레이아웃입니다. (이것은 성공적으로 작동했습니다.)
<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">
사용자 정의 보기를 사용하여 이 작업을 수행할 수 있습니다.이 솔루션을 사용하면 프로젝트에 포함된 모든 색상의 그라데이션 모양을 완성할 수 있습니다.
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'
<?xml version="1.0" encoding="utf-8"?>
<gradient
android:angle="90"
android:startColor="@color/colorPrimary"
android:endColor="@color/colorPrimary"
android:centerColor="@color/white"
android:type="linear"/>
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topRightRadius="10dp"
android:topLeftRadius="10dp"/>
언급URL : https://stackoverflow.com/questions/5976805/android-linearlayout-gradient-background
'sourcecode' 카테고리의 다른 글
jQuery에서 null 객체를 확인하는 방법 (0) | 2023.08.02 |
---|---|
express.json()과 express.urlenced()는 무엇입니까? (0) | 2023.08.02 |
두 가지 범위 조건을 가진 쿼리에 대한 인덱싱을 수행하려면 어떻게 해야 합니까? (0) | 2023.08.02 |
Microsoft의 OWIN 구현에서 확장 메서드 CreatePerOwinContext의 목적은 무엇입니까? (0) | 2023.08.02 |
개미매처() 대 개미매처()의 스프링 보안 적용 (0) | 2023.08.02 |