【问题标题】:Change color for different parts of the app更改应用程序不同部分的颜色
【发布时间】:2015-03-27 22:19:16
【问题描述】:

我的作业有问题。我的 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>

请帮忙

【问题讨论】:

  • 过去你不能在运行时动态改变你的主题,现在可能仍然如此。如果您在onCreate 中更改主题,这是可以接受的。但是要查看对主题的更改,您需要重新创建Activity。我不知道这是否已经改变。根据限制的不同,您可以通过调用每个视图并更改颜色。
  • 据我所知,这些限制仍然适用。该教程使用了每次按下按钮时都会重新启动的辅助活动,然后所有其他活动都从它们派生。但是,正如我所说,那样我只能更改背景、按钮或同时更改两者。而且我需要能够单独更改按钮和背景,而不是一一更改。

标签: android colors background-color


【解决方案1】:

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>




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

【讨论】:

  • 问题不在于如何选择颜色。我有 6 个按钮,每个按钮都有一个颜色。背景也是如此。我的问题是如何更改应用程序中所有按钮的颜色,而不是一一进行...
  • 我知道该怎么做。我正在寻找一种更改颜色的方法,而不必为每个 onClickListener 枚举每个按钮和背景...
  • @user3137018 : 我希望我能更好地理解你
  • @LanccePreston 我会试着解释一下......我想要的是一种改变颜色的方法,而不是这样做:relativeLayout.setBackgroundColor(Color.parseColor("#DC143C")); Green.setBackgroundColor(Color.RED); Yellow.setBackgroundColor(Color.RED); Blue.setBackgroundColor(Color.RED); 每次点击。我想要一种方法来更改所有按钮和所有背景的“主题”。我不想一一改变,因为太多了。
  • @user3137018:好的。我会回复你的!
【解决方案2】:

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

找到的解决方案似乎是一种介于一种方法之间的好方法(这不起作用,因为无法在一个活动中编辑与其他活动相关联的布局属性)和我在这里试图实现的目标.

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

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2013-05-27
    • 1970-01-01
    相关资源
    最近更新 更多