Android LinearLayout Gradient Tło

Mam problem z nałożeniem gradientowego tła na LinearLayout.

To powinno być stosunkowo proste z tego, co czytałem, ale to po prostu nie wydaje się działać. W celach informacyjnych rozwijam się na 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>

Jeśli zmienię @drawable / header_bg na kolor-np. #FF0000 to działa idealnie. Przegapiłem coś oczywistego?

Author: daksh21ubuntu, 2011-05-12

7 answers

Ok udało mi się to rozwiązać za pomocą selektora. Zobacz kod poniżej:

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>
Mam nadzieję, że to pomoże komuś, kto ma ten sam problem.
 359
Author: Genesis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-13 14:09:56

Możliwe jest również posiadanie trzeciego koloru (center). I różne rodzaje kształtów.

Na przykład w 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>

To daje czarno-szaro-czarny (od lewej do prawej) , który jest moim ulubionym ciemnym tłem atm.

Pamiętaj, aby dodać gradient.xml jako tło w układzie xml:

android:background="@drawable/gradient"

Możliwe jest również obracanie za pomocą:

Angle= " 0 "

Daje pionową linię

I z

Angle = " 90 "

Daje poziomą linię

Możliwe kąty to:

0, 90, 180, 270.

Istnieje również kilka różnych kształtów:

Android: shape= "rectangle"

Zaokrąglony kształt:

Android: shape= "oval"

I pewnie jeszcze kilka. Mam nadzieję, że to pomoże, zdrówko!
 40
Author: Sindri Þór,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-26 09:13:28

Spróbuj usunąć Androida: gradientRadius= "90". Oto jeden, który działa dla mnie:

<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>
 17
Author: Vincent Mimoun-Prat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-12 10:51:48

W pliku XML Drawable:

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

W pliku układu: 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>

Tutaj wpisz opis obrazka

 16
Author: Pacific P. Regmi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-02 03:04:54

Moim problemem byłorozszerzenie xml nie zostało dodane do nazwy pliku nowo utworzonego pliku XML. Dodawanie .rozszerzenie xml naprawiło mój problem.

 5
Author: kwahn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-06 18:06:01

Nie wiem, czy to komuś pomoże, ale mój problem polegał na tym, że próbowałem ustawić gradient na właściwość " src " ImageView w taki sposób:

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

Nie jestem w 100% pewien, dlaczego to nie zadziałało, ale teraz zmieniłem i umieściłem drawable w właściwości "background" rodzica ImageView, który jest RelativeLayout w moim przypadku, jak tak: (to działało pomyślnie)

<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">
 1
Author: instanceof,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-27 11:09:01

Sprawdziłbym Twój kanał alfa na Twoich gradientowych kolorach. Dla mnie, kiedy testowałem mój kod, miałem źle ustawiony kanał alfa na kolorach i nie działało dla mnie. Jak już mam ustawiony kanał alfa to wszystko działa!

 0
Author: Ray Hunter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-23 20:55:08