简体   繁体   English

Android:更改活动的背景颜色(主视图)

[英]Android: Changing Background-Color of the Activity (Main View)

I want to change the background color of my Main-View (not a Button or a Text-View) just the real background which is usually black... I got this code:我想改变我的主视图(不是按钮或文本视图)的背景颜色,只是通常是黑色的真实背景......我得到了这个代码:

view.setBackgroundColor(0xfff00000);

This is inside an OnClickListener , but it just changes the background of the Button.这是在OnClickListener ,但它只是改变了 Button 的背景。

Try creating a method in your Activity something like...尝试在您的Activity创建一个方法,例如...

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

Then call it from your OnClickListener passing in whatever colour you want.然后从你的 OnClickListener 中调用它,传入你想要的任何颜色。

i don't know if it's the answer to your question but you can try setting the background color in the xml layout like this.我不知道这是否是您问题的答案,但您可以尝试像这样在 xml 布局中设置背景颜色。 It is easy, it always works这很容易,它总是有效

<?xml version="1.0" encoding="utf-8"?>

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 android:background="0xfff00000"

  >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>

You can also do more fancy things with backgrounds by creating an xml background file with gradients which are cool and semi transparent, and refer to it for other use see example below:您还可以通过创建一个带有冷色和半透明渐变的 xml 背景文件来使用背景做更多花哨的事情,并参考它的其他用途,请参见下面的示例:

the background.xml layout background.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:angle="90"
            android:startColor="#f0000000"
            android:endColor="#ff444444"
            android:type="linear" />
    </shape>
</item>
</selector>

your layout你的布局

<?xml version="1.0" encoding="utf-8"?>

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 android:background="@layout/background"


    >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>

Just add this below one line code in the XML file of that corresponding activity:只需在相应活动的 XML 文件中的一行代码下方添加此代码:

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

it will help you for sure.它肯定会帮助你。

First Method第一种方法

 View someView = findViewById(R.id.randomViewInMainLayout);// get Any child View

  // Find the root view
  View root = someView.getRootView()

  // Set the color
  root.setBackgroundColor(getResources().getColor(android.R.color.red));

Second Method第二种方法

Add this single line after setContentView(...);在 setContentView(...); 之后添加这一行

getWindow().getDecorView().setBackgroundColor(Color.WHITE);

Third Method第三种方法

set background color to the rootView将背景颜色设置为 rootView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="@+id/rootView"
</LinearLayout>

Important Thing重要的事情

rootView.setBackgroundColor(0xFF00FF00); //after 0x the other four pairs are alpha,red,green,blue color. 

You can also try and provide an Id for the main layout and change the background of that through basic manipulation and retrieval.您还可以尝试为主布局提供一个 ID,并通过基本操作和检索来更改其背景。 Eg:例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hello"

Which can then be followed by accessing through R.id.hello.... Pretty basic and I hope this does help :)然后可以通过 R.id.hello 访问......非常基本,我希望这确实有帮助:)

I just want to add my 2 cents.我只想加上我的 2 美分。 I had the same goal (to change the background color from the .java class).我有同样的目标(从 .java 类更改背景颜色)。 But none of the above methods worked for me.但是以上方法都不适合我。

Issue was, that I set the background color inside the layout .xml file with android:background="@color/colorGray" :问题是,我使用android:background="@color/colorGray"在布局 .xml 文件中设置了背景颜色:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorGray">

So I just deleted particular line:所以我只是删除了特定的行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

Now I (you) can set the color from the code with:现在我(你)可以从代码中设置颜色:

getWindow().getDecorView().setBackgroundColor(Color.GRAY);

Just go to the activity_main.xml file.只需转到activity_main.xml 文件。 You will see an attributes panel on the right hand side, there you will find an attribute called "background".您将在右侧看到一个属性面板,在那里您会找到一个名为“背景”的属性。 You can set the color over there.你可以在那里设置颜色。

if you put your full code here so i can help you.如果你把你的完整代码放在这里,我可以帮你。 if your setting the listener in XML and calling the set background color on View so it will change the background color of the view means it ur Botton so put ur listener in ur activity and then change the color of your view如果您在 XML 中设置侦听器并在 View 上调用设置的背景颜色,那么它将更改视图的背景颜色意味着它是您的 Botton 所以将您的侦听器放在您的活动中,然后更改您的视图的颜色

Try this:试试这个:

 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimaryDark" </androidx.constraintlayout.widget.ConstraintLayout> />

你应该在 xml 中写,其中背景我

android:background="@drawable/imgname"

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

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