0

当我单击按钮时,我正在尝试更改视图背景的颜色,但是应用程序无法使用我现在拥有的代码打开我不知道问题是什么..

代码:

package on.click.button;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends ActionBarActivity {

        private ImageButton imagebutton;
        private ImageButton imagebuttonRED;
        private ImageButton imagebuttonBLUE;
        private ImageButton imagebuttonYELW;
        private ImageButton imagebuttonGRN;
        private View layout;


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


            imagebuttonRED = (ImageButton) findViewById(R.id.imageButton1);
            imagebuttonBLUE = (ImageButton) findViewById(R.id.imageButton3);
            imagebuttonYELW = (ImageButton) findViewById(R.id.imageButton2);
            imagebuttonGRN = (ImageButton) findViewById(R.id.imageButton4);

            layout = (View) findViewById(R.layout.activity_main);

            imagebutton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    setContentView(R.layout.activity_main);
                }
            });

            imagebuttonRED.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.GREEN);
                }
            });

            imagebuttonBLUE.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.RED);
                }
            });
            imagebuttonYELW.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.YELLOW);
                }
            });
            imagebuttonGRN.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.BLUE);
                }
            });

}
}

main_activity.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.aventus.delaatstestap.MainActivity" >

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton2"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/imageButton2"
        android:src="@drawable/blauw" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/rood" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:src="@drawable/flash" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/groen" />

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton4"
        android:layout_centerHorizontal="true"
        android:background="#FF0000" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_alignRight="@+id/imageButton1"
        android:layout_marginRight="19dp"
        android:src="@drawable/min" />

    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_marginLeft="18dp"
        android:src="@drawable/plus" />

</RelativeLayout>

请帮忙!

4

5 回答 5

2

在drawable中定义backcolor.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
     android:drawable="@drawable/clicked" />
<item  android:state_focused="false" 
  android:drawable="@drawable/normal" />
</selector>

可绘制对象中的 normal.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
</shape>

可绘制对象中的 Clicked.xml

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
 <solid android:color="#FF1A47"/>      
 </shape>

在布局中设置背景

android:background="@drawable/background"

和 Onclicklistner

 ll.setBackgroundColor(Color.RED);
于 2014-11-10T10:46:59.497 回答
0

问题似乎在这一行

 layout = (View) findViewById(R.layout.activity_main);

您正在尝试 findViewById 一个 xml 文件。所以你所要做的就是在activity_main中给你的父母一个id,然后通过这一行找到那个视图

RelativeLayout layout = (RelativeLayout) findViewById(R.layout.yourParentId);// since the parent is RelativeLayout

然后改变它的背景颜色

于 2014-11-10T10:44:25.047 回答
0

首先在 XML 中为您的 RelativeLayout 分配 id,

android:id="@+id/rlyt"

然后在java文件的第31行进行更正,即

            layout = (View)findViewById(R.layout.activity_main); 

            layout = (View)findViewById(R.id.rlyt);

并且不要在 OnClick 中使用 setContentView

于 2014-11-10T11:08:50.877 回答
0

您需要进行两项更改。按照步骤执行此操作。

第 1 步:将id添加到主要活动的RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.aventus.delaatstestap.MainActivity" >

第 2 步:使用该 ID 更改背景颜色。

relLayout = (View) findViewById(R.layout.rootLayout);
//to change color
relLayout.setBackgroundColor(Color.GREEN);
于 2014-11-10T10:43:09.840 回答
-1

像这样在您的 RelativeLayout 中放置一个 Id

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:id="@+id/viewId"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  tools:context="nl.aventus.delaatstestap.MainActivity" >

在你的活动中这样做:

                  RelativeLayout  layout = (RelativeLayout) findViewById(R.id.viewId);
                  ImageButton imagebutton = (ImageButton)findViewById(R.id......);   

                  imagebutton.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {
                         setContentView(R.layout.activity_main);
            }
        });
于 2014-11-10T10:58:14.470 回答