当我点击一个按钮时,我正在尝试更改我的视图背景颜色但是应用程序无法使用我现在拥有的代码打开我不知道问题是什么..
代码:
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>
请帮忙!
答案 0 :(得分:1)
在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>
drawable
中的normal.xml<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
</shape>
drawable
中的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);
答案 1 :(得分: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);
答案 2 :(得分:0)
问题似乎在这一行
layout = (View) findViewById(R.layout.activity_main);
您正在尝试查找ViewById xml文件。因此,您所要做的就是在activity_main中为您的父项提供一个id,然后通过此行找到该视图
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.yourParentId);// since the parent is RelativeLayout
然后更改其背景颜色
答案 3 :(得分: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
答案 4 :(得分:-1)
将Id放入您的RelativeLayout中,就像这样
<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);
}
});