Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法在onCreate()方法内的TextView中实现linerGradient()_Android_Textview_Oncreate_Linear Gradients - Fatal编程技术网

Android 无法在onCreate()方法内的TextView中实现linerGradient()

Android 无法在onCreate()方法内的TextView中实现linerGradient(),android,textview,oncreate,linear-gradients,Android,Textview,Oncreate,Linear Gradients,我想在我的文本视图中实现linearGradient()。我希望在加载活动时,将linearGradient()应用到我的文本视图中。我可以在一个按钮点击监听器中实现它,但是每当我在onCreate()方法中实现它时,线性渐变就不起作用了。下面是我的xml布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.andr

我想在我的文本视图中实现
linearGradient()
。我希望在加载活动时,将
linearGradient()
应用到我的文本视图中。我可以在一个按钮点击监听器中实现它,但是每当我在
onCreate()
方法中实现它时,线性渐变就不起作用了。下面是我的xml布局:

<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"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="70sp" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="63dp"
        android:text="Apply Gradient Text" />
</RelativeLayout>
GradientManager.java文件:

import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    private TextView tv;
    private Button btn;
    private int mWidth;
    private int mHeight;
    private Shader shader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(this);
        tv.setText(" Instagram  ");
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
        tv.setTypeface(face);

    }

    public void onClick(View view) {
        mWidth=tv.getWidth();
        mHeight=tv.getHeight();
        Point size = new Point(mWidth,mHeight);
        GradientManager gm= new GradientManager(getApplicationContext(),size);
        shader=gm.getRandomLinearGradient();
        tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        tv.getPaint().setShader(shader);
    }
}
import android.content.Context;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Point;
import android.graphics.Shader;


public class GradientManager {
    private Point mSize;

    public GradientManager(Context context, Point size){
        this.mSize = size;
    }

    protected LinearGradient getRandomLinearGradient(){

        LinearGradient gradient = new LinearGradient(0, 0, mSize.x, mSize.y,
                new int[] {Color.parseColor("#6656C8"), Color.parseColor("#8E33A9"),Color.parseColor("#BB328C"), Color.parseColor("#ED4B3E"),
                        Color.parseColor("#FA8031"), Color.parseColor("#FEC65C"), Color.parseColor("#FFD374") },null,
                Shader.TileMode.MIRROR
        );
        return gradient;
    }
}
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tv;
    private int mWidth;
    private int mHeight;
    private Shader shader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        tv.setText(" Instagram  ");
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
        tv.setTypeface(face);
        mWidth=tv.getWidth();
        mHeight=tv.getHeight();
        Point size = new Point(mWidth,mHeight);
        GradientManager gm= new GradientManager(getApplicationContext(),size);
        shader=gm.getRandomLinearGradient();
        tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        tv.getPaint().setShader(shader);       
    }   
}
活动开始时的屏幕截图:

按下按钮时的屏幕截图:

下面给出了我想要实现的(我删除了按钮):

MainActivity.java文件:

import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    private TextView tv;
    private Button btn;
    private int mWidth;
    private int mHeight;
    private Shader shader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(this);
        tv.setText(" Instagram  ");
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
        tv.setTypeface(face);

    }

    public void onClick(View view) {
        mWidth=tv.getWidth();
        mHeight=tv.getHeight();
        Point size = new Point(mWidth,mHeight);
        GradientManager gm= new GradientManager(getApplicationContext(),size);
        shader=gm.getRandomLinearGradient();
        tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        tv.getPaint().setShader(shader);
    }
}
import android.content.Context;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Point;
import android.graphics.Shader;


public class GradientManager {
    private Point mSize;

    public GradientManager(Context context, Point size){
        this.mSize = size;
    }

    protected LinearGradient getRandomLinearGradient(){

        LinearGradient gradient = new LinearGradient(0, 0, mSize.x, mSize.y,
                new int[] {Color.parseColor("#6656C8"), Color.parseColor("#8E33A9"),Color.parseColor("#BB328C"), Color.parseColor("#ED4B3E"),
                        Color.parseColor("#FA8031"), Color.parseColor("#FEC65C"), Color.parseColor("#FFD374") },null,
                Shader.TileMode.MIRROR
        );
        return gradient;
    }
}
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tv;
    private int mWidth;
    private int mHeight;
    private Shader shader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        tv.setText(" Instagram  ");
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
        tv.setTypeface(face);
        mWidth=tv.getWidth();
        mHeight=tv.getHeight();
        Point size = new Point(mWidth,mHeight);
        GradientManager gm= new GradientManager(getApplicationContext(),size);
        shader=gm.getRandomLinearGradient();
        tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        tv.getPaint().setShader(shader);       
    }   
}
GradientManager.java的内容不变,只有按钮从我的布局中删除。上面的代码不显示任何渐变颜色,而是使用线性渐变颜色参数[color.parseColor(“#6656C8”)]的第一种颜色显示整个textView

这是我的截图:


有人能帮我查一下密码吗?我错过了什么?感谢您的帮助

在onCreate期间,您正在使用tv.getWidth和getHeight。当时尚未测量TextView,因此这些值无效。İ如果必须获得渐变的宽度和高度,则应在完成测量步骤后应用渐变(即使用OnGlobalYoutListener)。

实际上,我需要一种在onCreate()中应用线性渐变的方法。我到处找,但没找到。所以我必须输入硬编码值,而不是tv.width和tv.height。你能给我一个解决方案来实现同样的功能吗?这样我就得到了集合text()中给定的任何文本视图文本的长度和宽度。即使我在布局xml文件中添加了一些值,此代码也无法工作。