2

我有一个简单的程序,可以在单击按钮后更改背景颜色,但它不起作用

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    
        }
    });
    }
}
4

4 回答 4

3

试试这个,

主要的.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);

        }
    });
    }
}
于 2012-06-30T02:58:39.720 回答
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>
于 2012-06-30T03:11:55.183 回答
1

您必须创建一个 xml 文件(选择器文件)并将其放在 res(res->drawable->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

于 2014-07-02T06:55:38.713 回答
0

使用这个,这对我有用:

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));
于 2013-09-06T07:37:11.117 回答