안드로이드 LinearLayout 그라데이션 배경 android:startColor=”#FFFF0000″

LinearLayout에 그라데이션 배경을 적용하는 데 문제가 있습니다.

이것은 내가 읽은 것에서 비교적 간단해야하지만 작동하지 않는 것 같습니다. 참고로 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_header.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_header.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_header_selector.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>

이것은 내가 가장 좋아하는 어두운 배경 atm 인 검은 색-회색-검은 색 (왼쪽에서 오른쪽으로)을 제공합니다.

레이아웃 xml에서 gradient.xml을 배경으로 추가해야합니다.

android:background="@drawable/gradient"

다음을 사용하여 회전 할 수도 있습니다.

angle = “0”

당신에게 수직선을 제공합니다

angle = “90”

당신에게 수평선을 제공합니다

가능한 각도는 다음과 같습니다.

0, 90, 180, 270.

또한 몇 가지 다른 모양이 있습니다.

android : shape = “rectangle”

둥근 모양 :

android : shape = “oval”

그리고 아마도 몇 가지 더.

도움이 되길 바랍니다.


답변

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 / gradient_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 : gradientRadius = “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>


답변

내 문제는 .xml 확장자가 새로 작성된 XML 파일의 파일 이름에 추가되지 않았다는 것입니다. .xml 확장명을 추가하면 문제가 해결되었습니다.


답변

Kotlin을 사용하면 단 두 줄로 할 수 있습니다

배열에서 색상 값 변경

                  val gradientDrawable = GradientDrawable(
                        GradientDrawable.Orientation.TOP_BOTTOM,
                        intArrayOf(Color.parseColor("#008000"),
                                   Color.parseColor("#ADFF2F"))
                    );
                    gradientDrawable.cornerRadius = 0f;

                   //Set Gradient
                   linearLayout.setBackground(gradientDrawable);

결과

여기에 이미지 설명을 입력하십시오


답변

이것이 누군가에게 도움이 될지 모르겠지만 내 문제는 그래디언트를 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 부모의 “background”속성에 드로어 블을 넣습니다.이 경우 내 RelativeLayout입니다.

<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">