简体   繁体   English

Android应用程序背景颜色按钮

[英]Android app background Colour Button

Hello again I have another question regarding my hello world app I want to change the background when A button is pressed so I did this: 再次您好,我还有一个关于我的hello world应用程序的问题,我想在按下A按钮时更改背景,所以我这样做了:

    public void onclick01(View View)  
       {  
           View.setBackgroundColor(Color.GREEN);

       } 

But that changes the background colour of the button and not the whole app. 但这会更改按钮的背景颜色,而不是整个应用程序。


Edit 编辑

I have two more questions. 我还有两个问题。

1) How would I set 1)我该如何设置

View.setBackgroundColor(Color.GREEN);

to something like: 像这样:

View.setBackgroundColor(Color.RANDOM);

2) How would I do the same to change the text colour? 2)我该怎么做才能更改文字颜色? something like: 就像是:

View.setTextColor(Color.GREEN);?

main_act.xml main_act.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />

</LinearLayout>

activity 活动

public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button b1;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_act);
    layout=(LinearLayout)findViewById(R.id.layout);
    blueButton=(Button)findViewById(R.id.b1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
        // TODO Auto-generated method stub
        layout.setBackgroundColor(Color.BLUE);

    }
});
}
}

If you want to set through xml, then you need to do as below: 如果要通过xml进行设置,则需要执行以下操作:

   android:background="@android:color/green"

in case if you decide to use android's default color code or if you have colors specified in colors.xml, then use 如果您决定使用android的默认颜色代码或在colors.xml中指定了颜色,则使用

    android:background="@colors/green"

If you want to do programmatically, then do: 如果要以编程方式执行操作,请执行以下操作:

     LinearLayout linearlayout=(LinearLayout) findViewById(R.layout.yourlayout);
     linearlayout.setBackgroundColor(Color.GREEN);

Here View reference your Button's View. 此处的视图引用了按钮的视图。 So you need to create object for parent layout an then 因此,您需要为父级布局创建一个对象,然后

layout.setBackgroundColor(Color.GREEN);
public void setActivityBackgroundColor(int color) {
 view = this.getWindow().getDecorView();
 view.setBackgroundColor(color);
}

Then call it from your OnClickListener passing in whatever color you want. 然后从您的OnClickListener调用它,并传递您想要的任何颜色。

You should have to use xml for this situation 在这种情况下,您必须使用xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_pressed="true">
    <shape android:shape="rectangle">
       <solid android:color="yourColor"/>
    </shape>
</item>

<item>
    <shape android:shape="rectangle">
        <solid android:color="yourColor"/>
    </shape>
</item>

</selector>

Use Selector you will get button press action. 使用Selector您将获得按钮按下动作。

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/button_background" android:state_pressed="false"/>
 <!-- default -->
 <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
 <!-- pressed -->
</selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM