更改应用程序不同部分的颜色

时间:2015-01-28 20:42:18

标签: android colors background-color

我的作业有问题。我的Android应用程序应该改变我在我的应用程序中的所有布局的背景颜色(顺便说一下我有9个)按一个按钮并通过按另一个按钮更改应用程序中所有按钮的颜色。我不知道如何做到这一点(编辑)除了一个一个地改变它们,我不想要,因为我有6种不同的颜色和很多按钮。

我找到了关于动态更改主题的教程, http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html 但这样我只能同时改变其中一个或两个。

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />
<uses-permission  android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:enabled="false" android:vmSafeMode="false">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Call"></activity>
    <activity android:name=".City"></activity>
    <activity android:name=".Map"></activity>
    <activity android:name=".Date"></activity>
    <activity android:name=".Color"></activity>
    <activity android:name=".Sms"></activity>
    <activity android:name=".Developer"></activity>
    <activity android:name=".Sms2"></activity>
</application>

请帮忙

2 个答案:

答案 0 :(得分:0)

MainActivity.java

package com.mavenmaverick.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends ActionBarActivity {

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

    final Button Red = (Button) findViewById(R.id.buttonred);
    final Button Green = (Button) findViewById(R.id.buttongreen);
    final Button Yellow = (Button) findViewById(R.id.buttonyellow);
    final Button Blue = (Button) findViewById(R.id.buttonblue);
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    Red.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            relativeLayout.setBackgroundColor(Color.parseColor("#DC143C"));
            Green.setBackgroundColor(Color.RED);
            Yellow.setBackgroundColor(Color.RED);
            Blue.setBackgroundColor(Color.RED);


        }
    });

    Green.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            relativeLayout.setBackgroundColor(Color.parseColor("#26D840"));
            Red.setBackgroundColor(Color.GREEN);
            Yellow.setBackgroundColor(Color.GREEN);
            Blue.setBackgroundColor(Color.GREEN);

        }
    });

    Yellow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            relativeLayout.setBackgroundColor(Color.parseColor("#FFDB58"));
            Green.setBackgroundColor(Color.YELLOW);
            Blue.setBackgroundColor(Color.YELLOW);
            Red.setBackgroundColor(Color.YELLOW);

        }
    });

    Blue.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            relativeLayout.setBackgroundColor(Color.parseColor("#0047AB"));
            Green.setBackgroundColor(Color.BLUE);
            Yellow.setBackgroundColor(Color.BLUE);
            Red.setBackgroundColor(Color.BLUE);

        }
    });
}

}



activity_main.xml     

<Button
    android:id="@+id/buttonyellow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/buttongreen"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:background="#FFDB58"
    android:text="All Yellow" />

<Button
    android:id="@+id/buttonred"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttongreen"
    android:layout_alignParentTop="true"
    android:layout_marginTop="38dp"
    android:background="#DC143C"
    android:text="All Red" />

<Button
    android:id="@+id/buttongreen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonyellow"
    android:layout_below="@+id/buttonred"
    android:layout_marginTop="46dp"
    android:background="#26D840"
    android:text="All Green" />

<Button
    android:id="@+id/buttonblue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonyellow"
    android:layout_below="@+id/buttonyellow"
    android:layout_marginTop="46dp"
    android:background="#0047AB"
    android:text="All Blue" />
</RelativeLayout>


enter image description here



现在,当您点击任意buttons时,background-color和所有buttons的颜色都会根据button上的信息发生变化。您可以根据所需的按钮和颜色数量,通过适当的编辑重复使用代码。

答案 1 :(得分:0)

在放弃这个并逐一尝试后,我遇到了另一个问题。这里解释并回答了Change layout background color on button click

找到的解决方案似乎是逐一方法之间的一种好方法(因为无法编辑与单个活动中的其他活动相关联的布局属性,因此无法工作)以及我在此尝试实现的目标。

这不是很时髦,因为按钮变成了一个彩色矩形,但现在对我来说已经足够了。

感谢您的帮助!

相关问题