2017-03-09 121 views
1

我正在尝试更改我的android应用程序中背景的颜色。当我按下按钮时,它将更改该特定活动的颜色,而不是所有其他活动的颜色。有没有办法改变它的整个应用程序?更改活动背景的颜色

screen

public class colorPicker extends AppCompatActivity { 

    View view; 

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

     view = this.getWindow().getDecorView(); 

    } 

    public void goPink(View v){ 

     view.setBackgroundResource(R.color.Pink); 
    } 

    public void goGreen(View v){ 

     view.setBackgroundResource(R.color.Green); 
    } 

    public void goYellow(View v){ 
     view.setBackgroundResource(R.color.Yellow); 

    } 
} 
+0

优先保存bg颜色并从首选项加载bg颜色 – UMESH0492

回答

0

你可以改变你应用主题的窗口背景颜色,不要使用活动的背景,或者您可以使用活动透明背景。

0

创建您的styles.xml一个主题,并添加以下到主题

<item name="android:windowBackground">@color/window_background 

,并在您

<application> 
... 
</application 

清单文件

+0

如果我想选择特定颜色作为开发人员,我相信这是一个解决方案。我希望用户通过在活动中按下一个按钮来选择一种颜色,然后根据这一点,颜色会改变。我希望我明确表达! –

+0

通过这个http://stackoverflow.com/questions/33987678/programmatically-change-the-value-of-a-color-resource-obtained-from-api-response –

0

设置android:theme="@style/YourTheme"在这种情况下,你需要在活动中添加少许变化。 在上的onCreate

if(getIntent().getExtras() != null) 
    { 
     int theme = getIntent().getIntExtra("theme", R.style.AppTheme); 
     setTheme(theme); 
     getApplication().setTheme(theme); 
     //recreate(); 
    } 

条件的onclick

if(condition) 
    getIntent().putExtra("theme", R.style.AppTheme2); 
    else 
    getIntent().putExtra("theme", R.style.AppTheme); 

,并保持2主题

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary"></item></style> 

和类似它只是更名为BaseTheme2第二个主题。

但是这不建议在运行时更改应用程序主题。

+0

在问题中没有“意图”。颜色来自按钮单击事件。 –