【问题标题】:Android: Set Random background color for textview on createAndroid:在创建时为 textview 设置随机背景颜色
【发布时间】:2020-03-13 04:55:59
【问题描述】:

我想要的是当我加载我的应用程序时,它可以从预定义的字符串列表中为每个 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>

【问题讨论】:

  • 发布您的 xml 布局文件
  • @Prajwal 检查一下
  • 使用给定的代码,您的应用程序应该会因NullPointerException 而崩溃。如果在setContentView() 之前调用,findViewById() 将返回 null。你确定那是你正在运行的实际代码吗?

标签: android random colors textview oncreate


【解决方案1】:

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

setContentView(R.layout.x);

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

更新修复:

确保在使用 findViewById 之类的方法之前,在 onCreate 方法的开头调用 super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)...这应该会导致 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) 
}

【讨论】:

  • 检查我的布局文件
  • 也发布您的整个活动
  • 无法用您发布的内容解决此问题,请发布您的整个活动。以及应用加载时的 logcat 输出。
  • 用修复更新了我的答案。
【解决方案2】:

您在 java 代码中指定: R.array.rainbow 而您将彩虹保存为 colorr.xml 属性!

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

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

如果 tat 解决了您的问题,请告诉我们

【讨论】:

    【解决方案3】:
    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)));
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-13
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 2017-04-14
      • 2013-10-14
      • 2013-08-04
      相关资源
      最近更新 更多