簡體   English   中英

Android:更改活動的背景顏色(主視圖)

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

我想改變我的主視圖(不是按鈕或文本視圖)的背景顏色,只是通常是黑色的真實背景......我得到了這個代碼:

view.setBackgroundColor(0xfff00000);

這是在OnClickListener ,但它只是改變了 Button 的背景。

嘗試在您的Activity創建一個方法,例如...

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

然后從你的 OnClickListener 中調用它,傳入你想要的任何顏色。

我不知道這是否是您問題的答案,但您可以嘗試像這樣在 xml 布局中設置背景顏色。 這很容易,它總是有效

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

您還可以通過創建一個帶有冷色和半透明漸變的 xml 背景文件來使用背景做更多花哨的事情,並參考它的其他用途,請參見下面的示例:

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>

你的布局

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

只需在相應活動的 XML 文件中的一行代碼下方添加此代碼:

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

它肯定會幫助你。

第一種方法

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

第二種方法

在 setContentView(...); 之后添加這一行

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

第三種方法

將背景顏色設置為 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>

重要的事情

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

您還可以嘗試為主布局提供一個 ID,並通過基本操作和檢索來更改其背景。 例如:

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

然后可以通過 R.id.hello 訪問......非常基本,我希望這確實有幫助:)

我只想加上我的 2 美分。 我有同樣的目標(從 .java 類更改背景顏色)。 但是以上方法都不適合我。

問題是,我使用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">

所以我只是刪除了特定的行:

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

現在我(你)可以從代碼中設置顏色:

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

只需轉到activity_main.xml 文件。 您將在右側看到一個屬性面板,在那里您會找到一個名為“背景”的屬性。 你可以在那里設置顏色。

如果你把你的完整代碼放在這里,我可以幫你。 如果您在 XML 中設置偵聽器並在 View 上調用設置的背景顏色,那么它將更改視圖的背景顏色意味着它是您的 Botton 所以將您的偵聽器放在您的活動中,然后更改您的視圖的顏色

試試這個:

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