【问题标题】:How to apply gradient for below kitkat devices in android如何在 android 中为以下 kitkat 设备应用渐变
【发布时间】:2016-09-05 12:53:03
【问题描述】:

我的应用由背景颜色渐变组成,我已经在棒棒糖和 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>

这是我的 MainActivity 样子

public class MainActivity extends AppCompatActivity {

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}

这是我的 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>

这是我得到的输出

output in both lollipop and kitkat devices

【问题讨论】:

    标签: android layout gradient


    【解决方案1】:

    我对你的代码做了一些修改,

    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 设备中运行良好,这是输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多