如何在android中为低于kitkat的设备应用渐变

如何在android中为低于kitkat的设备应用渐变,android,layout,gradient,Android,Layout,Gradient,我的应用程序由背景色渐变组成,我已经在棒棒糖和kitkat设备上测试了该应用程序,我发现渐变适用于上面的棒棒糖设备,但在棒棒糖设备下不起作用。 这是我的gradient.xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:angle="90" android:centerColor="#

我的应用程序由背景色渐变组成,我已经在棒棒糖和kitkat设备上测试了该应用程序,我发现渐变适用于上面的棒棒糖设备,但在棒棒糖设备下不起作用。

这是我的gradient.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="90"
    android:centerColor="#4477C7"
    android:centerY="0"
    android:endColor="#303F9F"
    android:gradientRadius="775dp"
    android:startColor="#59C6FF"
    android:type="radial" />
<corners android:radius="0dp" /></shape>
public class MainActivity extends AppCompatActivity {

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_gradient"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:id="@+id/relativeLayout"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ravi.gradientexample.MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="Hello World!"
    android:textColor="@android:color/white" />
</RelativeLayout>
这是我的主要活动

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="90"
    android:centerColor="#4477C7"
    android:centerY="0"
    android:endColor="#303F9F"
    android:gradientRadius="775dp"
    android:startColor="#59C6FF"
    android:type="radial" />
<corners android:radius="0dp" /></shape>
public class MainActivity extends AppCompatActivity {

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_gradient"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:id="@+id/relativeLayout"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ravi.gradientexample.MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="Hello World!"
    android:textColor="@android:color/white" />
</RelativeLayout>

这是我得到的输出


我对您的代码做了一些修改

public class MainActivity extends AppCompatActivity {

private RelativeLayout relativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    GradientDrawable gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[]{0xFF59C6FF, 0xFF4477C7, 0xFF303F9F}); // Gradient Color Codes
    gradientDrawable.setCornerRadius(0f);                   // Setting Corner Radius
    gradientDrawable.setGradientRadius(775f);               // Setting Graidnet Radius
    gradientDrawable.setGradientCenter(0.5f, 0);
    gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT); // Gradient Type
    relativeLayout.setBackgroundDrawable(gradientDrawable);         //Setting Gradient To Layout
}}
它在我的kitkat设备中运行良好,下面是输出