Changing background color in Android SDK by clicki

2019-07-09 05:30发布

I have a simple program which is able to change the background color after clicking a button, but it does not work

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条回答
走好不送
2楼-- · 2019-07-09 05:32

Try With this,

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

        }
    });
    }
}
查看更多
forever°为你锁心
3楼-- · 2019-07-09 05:32

You have to create an xml file(selector file) and put it in drawable folder in res(res->drawable->yourselectorfile.xml. After that set xml file in button's background in your layout file

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>

Now set the above file in button's background.

<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textColor="@color/grey_text"
    android:background="@drawable/button_background_selector"/>

and for changing color use

<?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>

as your button_background_selector.xml

查看更多
Explosion°爆炸
4楼-- · 2019-07-09 05:34

Use this, this worked for me:

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));
查看更多
手持菜刀,她持情操
5楼-- · 2019-07-09 05:56

use

myLO=(LinearLayout)findViewById(R.id.main);

insteadof

myLO=(LinearLayout)findViewById(R.layout.main);

your layout must be like that

<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>
查看更多
登录 后发表回答