我想在应用程序中更改不同意图的颜色。对于一个意图操作栏颜色为红色,对于其他一些意图,操作栏颜色为黄色。
这是我的AndroidMenifest.xml,其中我更改了标签文本。
但是如何更改该文本背景的背景与应用程序的主屏幕相同。
看起来我做了改变,但它不起作用。 `package com.example.android.miwok;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout
file
setContentView(R.layout.activity_main);
TextView numbers = (TextView) findViewById(R.id.numbers);
// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is
clicked on.
@Override
public void onClick(View view) {
Intent numbersIntent = new Intent(MainActivity.this,
NumbersActivity.class);
startActivity(numbersIntent);
Intent i=new Intent(MainActivity.this,NumbersActivity.class);
i.putExtra("toolbarColor", Color.BLUE);
}
});
TextView family = (TextView) findViewById(R.id.family);
// Set a click listener on that View
family.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the family View is
clicked on.
@Override
public void onClick(View view) {
Intent familyIntent = new Intent(MainActivity.this,
FamilyMembersActivity.class);
startActivity(familyIntent);
}
});
TextView colors = (TextView) findViewById(R.id.colors);
// Set a click listener on that View
colors.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the colors View is
clicked on.
@Override
public void onClick(View view) {
Intent colorsIntent = new Intent(MainActivity.this,
ColorsActivity.class);
startActivity(colorsIntent);
}
});
TextView pharses = (TextView) findViewById(R.id.phrases);
// Set a click listener on that View
pharses.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is
clicked on.
@Override
public void onClick(View view) {
Intent pharsesIntent = new Intent(MainActivity.this,
PharsesActivity.class);
startActivity(pharsesIntent);
}
});
}
public void openNumberList(View view){
Intent viewNumber= new Intent(this,NumbersActivity.class);
startActivity(viewNumber);
}
public void openFamilyMembersList(View view){
Intent viewFamily= new Intent(this,FamilyMembersActivity.class);
startActivity(viewFamily);
}
public void openPharsesList(View view){
Intent viewPharses= new Intent(this,PharsesActivity.class);
startActivity(viewPharses);
}
public void openColorsList(View view){
Intent viewColor= new Intent(this,ColorsActivity.class);
startActivity(viewColor);
}
}
`
答案 0 :(得分:1)
这就是你想要的吗?如果是这样,请按照我的步骤。我会告诉你该怎么做。
首先,声明每个活动的样式。[res / values / styles.xml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- other styles ... -->
<style name="NumbersBg" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryNumbers</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkNumbers</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="FamilyMembersBg" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryFamilyMembers</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkFamilyMembers</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="ColorsBg" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryColors</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkColors</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="PhrasesBg" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryPhrases</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkPhrases</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
顺便说一句,上面提到的颜色应该在colors.xml [res / values / colors.xml]中更早地声明:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- other colors ... -->
<!-- Numbers -->
<color name="colorPrimaryNumbers">#fd8d28</color>
<color name="colorPrimaryDarkNumbers">#AE611C</color>
<!-- Family Members -->
<color name="colorPrimaryFamilyMembers">#3b913c</color>
<color name="colorPrimaryDarkFamilyMembers">#2C6B2C</color>
<!-- Colors -->
<color name="colorPrimaryColors">#87149e</color>
<color name="colorPrimaryDarkColors">#4C0C59</color>
<!-- Phrases -->
<color name="colorPrimaryPhrases">#26afc8</color>
<color name="colorPrimaryDarkPhrases">#186D7C</color>
</resources>
引用之前在AndroidManifest.xml中声明的样式。
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application>
<!-- other activities ... -->
<activity
android:name=".NumbersActivity"
android:theme="@style/NumbersBg" />
<activity
android:name=".FamilyMembersActivity"
android:theme="@style/FamilyMembersBg" />
<activity
android:name=".ColorsActivity"
android:theme="@style/ColorsBg" />
<activity
android:name=".PhrasesActivity"
android:theme="@style/PhrasesBg" />
</application>
</manifest>
运行它。
答案 1 :(得分:0)
您可以将颜色作为额外的颜色发送给活动。
Intent intent = ..;
intent.putExtra("toolbarColor", Color.BLUE);
在活动中,您希望工具栏颜色更改
int color = getIntent().getExtra("toolbarColor");
toolbar.setBackgroundDrawable(new ColorDrawable(color));