带数组的setBackgroundColor保持灰色,不会改变颜色

时间:2014-08-15 23:32:20

标签: android

我有一个带按钮的视图,当单击该按钮时,背景颜色应该更改为strings.xml中的一个十六进制代码:

    <string-array name="colorcode_array">
    <item>#3498db</item>
    <item>#2ecc71</item>
    <item>#9b59b6</item>
    <item>#f1c40f</item>
    <item>#1abc9c</item>
    <item>#2980b9</item>
    <item>#8e44ad</item>
    <item>#e41c1c</item>
    <item>#2ecca9</item>
    <item>#752ecc</item>
    <item>#4f2ecc</item>
    <item>#2eccc3</item>
    <item>#2ecc53</item>
    <item>#2ecc2e</item>
    <item>#5bcc2e</item>
    <item>#9ecc2e</item>
    <item>#cca12e</item>
    <item>#cc712e</item>
    <item>#f1c209</item>
    <item>#86f109</item>
    <item>#f11616</item>
    <item>#9c1818</item>
</string-array>

但是当点击它时,颜色变为浅灰色并且每次点击都会保持浅灰色。这是我的其余代码:

activity_my2.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textColor="#ffffffff"
    android:textSize="72sp"
    android:background="#00000000"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/thing1"
    android:id="@+id/textView"
    android:textColor="#ffffffff"
    android:textSize="36sp"
    android:gravity="center"
    android:layout_above="@+id/button"
    />


</RelativeLayout>

MyActivity2.java:

package com.MR.brd;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.MR.brd.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MyActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    final TextView tv = (TextView) findViewById(R.id.textView);
    Button n = (Button) findViewById(R.id.button);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    Typeface face = Typeface.createFromAsset(getAssets(),"OSP-DIN.ttf");
    tv.setTypeface(face);


final String[] values = getResources().getStringArray(R.array.things_array);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

    Random RAND=new Random();
    int position = RAND.nextInt(values.length);
    String nextValue = values[position];
    int colors[] = getResources().getIntArray(R.array.colorcode_array);
    int color = colors[position];
    tv.setText(nextValue);
    View layout = findViewById(R.id.layout_view);
    layout.setBackgroundColor(color);
        }
    });
 }
@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation4, R.anim.animation3);
    }
}

1 个答案:

答案 0 :(得分:1)

立即尝试此功能它应该适用于您班级中的一些更改,因此每次按下按钮时,您必须将该背景设置为RelativeaLayout的参考。

public class Sample2 extends Activity {

    private String[] colors;
    private String[] values;
    private TextView tv;
    private RelativeLayout rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_activity2);
        Button n = (Button) findViewById(R.id.button);
        tv = (TextView) findViewById(R.id.textView);
        rl = (RelativeLayout) findViewById(R.id.layout_view);

        Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
        n.setTypeface(typeface);
        Typeface face = Typeface.createFromAsset(getAssets(),"OSP-DIN.ttf");
        tv.setTypeface(face);

        values = getResources().getStringArray(R.array.things_array);
        colors = getResources().getStringArray(R.array.colorcode_array);

        n.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random RAND = new Random();
                int position = RAND.nextInt(colors.length);
                int position2 = RAND.nextInt(values.length);
                String nextValue = colors[position];
                String textValue = values[position2];

                tv.setText(textValue);
                rl.setBackgroundColor(Color.parseColor(nextValue));

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.animation4, R.anim.animation3);
    }
}