我有一个简单的程序,可以在单击按钮后更改背景颜色,但它不起作用
public class ChangeBackgroundActivity extends Activity {
/** Called when the activity is first created. */
Button blueButton;
LinearLayout myLO;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLO=(LinearLayout)findViewById(R.layout.main);
blueButton=(Button)findViewById(R.id.button1);
blueButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myLO.setBackgroundColor(0x0000FF); //blue color code #0000FF
}
});
}
}
答案 0 :(得分:3)
试试这个,
<强> main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="71dp"
android:text="changeColor" />
</LinearLayout>
<强> ChangeBackgroundActivity.java 强>
public class ChangeBackgroundActivity extends Activity {
/** Called when the activity is first created. */
Button blueButton;
LinearLayout myLO;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLO=(LinearLayout)findViewById(R.id.myLayout);
blueButton=(Button)findViewById(R.id.button1);
blueButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myLO.setBackgroundColor(Color.BLUE);
}
});
}
}
答案 1 :(得分:3)
使用
myLO=(LinearLayout)findViewById(R.id.main);
而不是
myLO=(LinearLayout)findViewById(R.layout.main);
你的布局必须像那样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
答案 2 :(得分:1)
您必须创建一个xml文件(选择器文件)并将其放在res中的drawable文件夹中(res-&gt; drawable-&gt; yourselectorfile.xml。之后在布局文件的按钮背景中设置xml文件
<强> button_background_selector.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/your_hover_image"/>
<item android:drawable="@drawable/your_simple_image" />
</selector>
现在在按钮的背景中设置上述文件。
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/grey_text"
android:background="@drawable/button_background_selector"/>
用于改变颜色用途
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
作为button_background_selector.xml
答案 3 :(得分:0)
使用它,这对我有用:
YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));