Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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
Java 在Android中更改每个屏幕的图像背景_Java_Android_Android Studio_Drawable_Setbackground - Fatal编程技术网

Java 在Android中更改每个屏幕的图像背景

Java 在Android中更改每个屏幕的图像背景,java,android,android-studio,drawable,setbackground,Java,Android,Android Studio,Drawable,Setbackground,我还是android新手,所以请尽可能仔细地解释,最好使用示例代码。我目前正在开发一个事实查询应用程序,当点击按钮时,它会简单地显示一个新的事实。每次显示新事实时,都会弹出相同的屏幕,但颜色不同。我这样做是通过将事实存储在数组中,并将颜色编号存储在数组中,然后随机匹配它们。当一个特定的(不是随机的颜色)事实弹出时,我如何将背景更改为自定义图像。我知道要设置第一幅图像的背景,我们只需编写android:background=“@drawable/(name_of_image)”我该怎么做,下面是我

我还是android新手,所以请尽可能仔细地解释,最好使用示例代码。我目前正在开发一个事实查询应用程序,当点击按钮时,它会简单地显示一个新的事实。每次显示新事实时,都会弹出相同的屏幕,但颜色不同。我这样做是通过将事实存储在数组中,并将颜色编号存储在数组中,然后随机匹配它们。当一个特定的(不是随机的颜色)事实弹出时,我如何将背景更改为自定义图像。我知道要设置第一幅图像的背景,我们只需编写android:background=“@drawable/(name_of_image)”我该怎么做,下面是我迄今为止编写的代码:不同的颜色存储在一个名为ColorWheel的新类中

package com.example.kharl.funfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by Kharl on 1/27/2015.
 */
public class ColorWheel {
    //Member variable (propoerties about the object)
    public String[] mColors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
    };


    String color="";
    //Method (abilities:things the object can do)
    public int getColor(){

        //Randomly select a fact
        Random randomGenerator =new Random(); // construct a new random generator
        int randomNumber =randomGenerator.nextInt(mColors.length);
        color = mColors[randomNumber];
        int colorAsInt = Color.parseColor(color);

        return colorAsInt;






    }
}
色轮

package com.example.kharl.funfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by Kharl on 1/27/2015.
 */
public class ColorWheel {
    //Member variable (propoerties about the object)
    public String[] mColors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
    };


    String color="";
    //Method (abilities:things the object can do)
    public int getColor(){

        //Randomly select a fact
        Random randomGenerator =new Random(); // construct a new random generator
        int randomNumber =randomGenerator.nextInt(mColors.length);
        color = mColors[randomNumber];
        int colorAsInt = Color.parseColor(color);

        return colorAsInt;






    }
}
Java

public class FunFactsActivity extends Activity {

        public static final String TAG =FunFactsActivity.class.getSimpleName();

        private FactBook mFactBook = new FactBook();
        private ColorWheel mColorWheel= new ColorWheel();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fun_facts);
            // Declare our view variables and assign them the views from the layout file
           final TextView factLabel = (TextView) findViewById(R.id.factTextView);
            final Button showFactButton= (Button) findViewById(R.id.showFactButton);
            final RelativeLayout relativelayout =(RelativeLayout) findViewById(R.id.relativeLayout);
            View.OnClickListener listener= new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String fact = mFactBook.getFact();


                    //Update the label with our dynamic fact
                    factLabel.setText(fact);
                   int color = mColorWheel.getColor();
                    relativelayout.setBackgroundColor(color);
                    showFactButton.setTextColor(color);


                }
            };
            showFactButton.setOnClickListener(listener);

    //Toast.makeText(this,"YAY! our activity was created",Toast.LENGTH_LONG).show();
    Log.d(TAG, "We are Logging from the oncreate method");
        }
    }
事实手册

package com.example.kharl.funfacts;

import java.util.Random;

/**
 * Created by Kharl on 1/26/2015.
 */
public class FactBook {
    //Member variable (propoerties about the object)
    public String[] mFacts = {
            "Ants stretch when they wake up in the morning.",
            "Ostriches can run faster than horses.",
            "Olympic gold medals are actually made mostly of silver.",
            "You are born with 300 bones; by the time you are an adult you will have 206.",
            "It takes about 8 minutes for light from the Sun to reach Earth.",
            "Some bamboo plants can grow almost a meter in just one day.",
            "The state of Florida is bigger than England.",
            "Some penguins can leap 2-3 meters out of the water.",
            "On average, it takes 66 days to form a new habit.",
            "Mammoths still walked the earth when the Great Pyramid was being built." };


    String fact="";
    //Method (abilities:things the object can do)
    public String getFact(){

        //Randomly select a fact
        Random randomGenerator =new Random(); // construct a new random generator
        int randomNumber =randomGenerator.nextInt(mFacts.length);
        fact = mFacts[randomNumber];

    return fact;
    }
}

将ImageView添加到layout.xml文件中

<ImageView
    android:id="@+id/myimage"
    android:layout_width="100dp"
    android:layout_height="118dp"
    android:layout_marginRight="8dp"
    android:maxHeight="100dp"
    android:maxWidth="100dp"
    android:scaleType="centerCrop"
    android:src="@drawable/name_of_image" />