[英]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.