Android:在创建时为textview设置随机背景颜色

时间:2019-11-17 19:03:47

标签: android random colors textview oncreate

我想要的是,当我加载我的应用程序时,它会从预定义的字符串列表中为每个textview随机具有特定的彩色背景。

 @Override
protected void onCreate(Bundle savedInstanceState) {

    int[] rainbow = getResources().getIntArray(R.array.rainbow);
    int randomAndroidColor = rainbow[new Random().nextInt(rainbow.length)];
    TextView lab = (TextView) findViewById(R.id.view1);
    lab.setBackgroundColor(randomAndroidColor) super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

colors.xml

<resources>


<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>


<integer-array name="rainbow">
    <item>@color/blue</item>
    <item>@color/purple</item>
    <item>@color/green</item>


</integer-array>

为什么它不起作用?

更新 这是我的布局文件。 我的应用加载了,但是什么也没出现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical">

    <TextView
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:text="@string/hello"
         />
    <TextView
        android:id="@+id/view2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:text="@string/hello"
        />
    <TextView
        android:id="@+id/view3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:text="@string/hello"
        />
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

您的代码可在我的设备上使用。您是否正确指定了布局?

setContentView(R.layout.x);

findViewById(R.id.view1)是否引用了正确的xml元素?

更新修补程序:

在使用super.onCreate(savedInstanceState)之类的方法之前,请确保先在onCreate方法的开头调用setContentView(R.layout.activity_main)findViewById ...这应该会导致NPE崩溃...

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

    int[] rainbow = getResources().getIntArray(R.array.rainbow);
    int randomAndroidColor = rainbow[new Random().nextInt(rainbow.length)];
    TextView lab = (TextView) findViewById(R.id.view1);
    lab.setBackgroundColor(randomAndroidColor) 
}

答案 1 :(得分:0)

您正在Java代码中指定R.array.rainbow,而将那条彩虹保存为colorr.xml属性!

转到res / arrays文件夹-创建Rainbow.xml文件,然后将那些数组元素添加到其中。

当您指定R.array.x时,调试器将开始在您的数组目录中搜索rainbow.xml文件!

让我们知道达人能否解决您的问题

答案 2 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<Integer> colors = new ArrayList<>();
    colors.add(getResources().getColor(R.color.с1));
    colors.add(getResources().getColor(R.color.с2));
    colors.add(getResources().getColor(R.color.с3));
    colors.add(getResources().getColor(R.color.с4));
    colors.add(getResources().getColor(R.color.с5));
    colors.add(getResources().getColor(R.color.с6));
    colors.add(getResources().getColor(R.color.с7));
    colors.add(getResources().getColor(R.color.с8));

    List<TextView> textViews = new ArrayList<>();
    textViews.add((TextView) findViewById(R.id.text1));
    textViews.add((TextView) findViewById(R.id.text2));
    textViews.add((TextView) findViewById(R.id.text3));
    textViews.add((TextView) findViewById(R.id.text4));
    textViews.add((TextView) findViewById(R.id.text5));
    textViews.add((TextView) findViewById(R.id.text6));

    Random random = new Random();

    for (TextView item: textViews) {
        item.setBackgroundColor(colors.get(random.nextInt(8)));
    }

}