【问题标题】:Gradinet not applying in kitkat devices android渐变不适用于kitkat设备android
【发布时间】:2016-08-22 06:18:01
【问题描述】:

我已经对activity_main.xml 应用了渐变,当我在marshmallow 设备上运行时应用了渐变,但是当我在kitkat 设备上运行时不应用渐变。这是activity_main.xml的样子

棉花糖用户界面屏幕:

Kitkat UI 屏幕:

这里是activity_main.xml源代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_gradient"
android:focusableInTouchMode="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</ScrollView>

这是我正在使用的渐变代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="90"
    android:centerColor="#295e7c"
    android:centerY="0"
    android:endColor="#303a70"
    android:gradientRadius="775dp"
    android:startColor="#56a68d"
    android:type="radial" />
<corners android:radius="0dp" />
</shape>

【问题讨论】:

    标签: android android-layout gradient android-6.0-marshmallow android-4.4-kitkat


    【解决方案1】:

    尝试从gradientRadius 中删除dp

    <gradient
        android:angle="90"
        android:centerColor="#295e7c"
        android:centerY="0"
        android:endColor="#303a70"
        android:gradientRadius="775"
        android:startColor="#56a68d"
        android:type="radial" />
    

    【讨论】:

    • @Karthik 不要删除渐变半径。仅将值从 775dp 更改为仅 775。仅删除 dp。
    • 是的,我已经按照你说的修改了代码,我在 kitkat 设备上工作得很好,但在 marshallow 设备上它看起来不太好。
    • 值 775 需要根据您的要求进行调整。
    【解决方案2】:

    我已经按照你说的实现了代码,它对 kitkat 和 marshmallow 版本都很好。有一个问题,如果您使用多个布局,那么您在这些类中调用此方法。

    ssv = (ScrollView) findViewById(R.id.ssv);
    
        if (Build.VERSION.SDK_INT <= 22) {
        GradientDrawable gradientDrawable = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[]{0xFF56a68d, 0xFF295e7c, 0xFF303a70});
        gradientDrawable.setCornerRadius(0f);
        gradientDrawable.setGradientRadius(775f);
        gradientDrawable.setGradientCenter(0.5f, 0);
        gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        ssv.setBackgroundDrawable(gradientDrawable);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多