【问题标题】:Trying to change main activity background color on button click尝试在单击按钮时更改主要活动背景颜色
【发布时间】:2013-06-05 16:28:10
【问题描述】:

这是我的主要内容:

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

    Button btn = (Button) findViewById(R.id.newgame_button);

    btn.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            changeBackground(v);
        }
    }); 
}

我试过以下方法,都失败了:

public void changeBackground(View v) 
{
      View someView = findViewById(R.id.main);
      View root = someView.getRootView();
      root.setBackgroundColor(getResources().getColor(R.color.red));
}

public void changeBackground(View v) 
{
      View root = v.getRootView();
      root.setBackgroundColor(getResources().getColor(R.color.red));
}

我在How to set background color of Activity to white programmatically?中搜索并找到了解决方法;发布的解决方案是:

  // Now get a handle to any View contained 
  // within the main layout you are using
  View someView = findViewById(R.id.randomViewInMainLayout);

  // Find the root view
  View root = someView.getRootView()

  // Set the color
  root.setBackgroundColor(android.R.color.red);

当我尝试android.R.color.red 时,eclipse 告诉我它应该是我示例中的形式。

我确实设法改变了按钮的背景:

public void changeBackground(View v) 
{
    v.setBackgroundColor(getResources().getColor(R.color.red));
}

所以,我很确定问题不在于:getResources().getColor(R.color.red)。我尝试了许多不同的方法,但我没有得到任何地方。作为参考,这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/LightCyan"
    android:id="@+id/main">

   <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:text="@string/welcome"
        android:gravity="center"
        android:layout_weight="3" />

   <LinearLayout
        android:layout_width="200dp"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_weight="2" > 

       <Button
           android:id="@+id/resume_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/resume" 
           android:background="@color/Plum"/>

       <Button
           android:id="@+id/newgame_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/new_game"
           android:background="@color/Plum" 
           android:onClick="changeBackground"/>

    </LinearLayout>

</LinearLayout> 

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 你可以试试this回答。

标签: android android-layout


【解决方案1】:
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    Button btn = (Button) findViewById(R.id.newgame_button);

    btn.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            changeBackground(v);
        }
    }); 
}

改成

   Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    actvity = this;
    Button btn = (Button) findViewById(R.id.newgame_button);

    btn.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {

           actvity.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);
        }
    }); 
}

【讨论】:

  • 它从android.R.id.content 中获取当前的View,因为你说的是​​activity = this?那么,android.R.id.content 是一个特殊的变量吗?
  • @SteveP。不是这样的。用于获取活动的内容。参考here。这里this 仅指当前活动。
【解决方案2】:

在我的应用程序中,我使用以下代码更改背景:

View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.BLACK);

试试吧,也许它对你有用。

【讨论】:

  • 如果这不起作用,那么您可能有一些东西覆盖了您的活动背景。我发布的代码更改了根的背景,这意味着如果您的活动中有任何布局,则由于视图层次结构,此更改将不可见。
  • 我在上面发布了我的所有代码。没有任何相关的缺失。
  • 那我真的很好奇为什么我的代码不适合你...奇怪
  • 我对android比较陌生,所以我不知道。不过,我在回答中所做的最终奏效了。
【解决方案3】:

想通了:

public void changeBackground(View v) 
{
      View root = findViewById(R.id.main);
      root.setBackgroundColor(getResources().getColor(R.color.red));
}

显然,当您处理主要的View 时,使用getRootView() 并不好。我不知道,但这有效。

【讨论】:

    【解决方案4】:

    @Steve P.

    改变这个

      public void changeBackground(View v)  {
        v.setBackgroundColor(getResources().getColor(R.color.red)); }
    

    有了这个

     public void ChangeBackground(View v)
    {
        View parentsView = (View) v.getParent();
        parentsView.setBackgroundColor(Color.RED);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-14
      • 2014-10-09
      • 2017-08-02
      • 2014-10-10
      • 2019-04-14
      • 1970-01-01
      • 2015-11-06
      • 2014-10-07
      相关资源
      最近更新 更多