Java Android:更改活动的背景色(主视图)
我想改变我的主视图(不是一个按钮或文本视图)的背景颜色,只是真实的背景,通常是黑色。。。我得到了这个密码:Java Android:更改活动的背景色(主视图),java,android,colors,Java,Android,Colors,我想改变我的主视图(不是一个按钮或文本视图)的背景颜色,只是真实的背景,通常是黑色。。。我得到了这个密码: view.setBackgroundColor(0xfff00000); 这是在一个OnClickListener中,但它只是更改按钮的背景 试着在你的活动中创建一个方法,比如 public void setActivityBackgroundColor(int color) { View view = this.getWindow().getDecorView(); v
view.setBackgroundColor(0xfff00000);
这是在一个
OnClickListener
中,但它只是更改按钮的背景 试着在你的活动中创建一个方法,比如
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
然后从你的OnClickListener调用它,输入你想要的任何颜色。如果你把完整的代码放在这里,我可以帮你。如果您在XML中设置侦听器并调用set background color on View(在视图中设置背景色),那么它将更改视图的背景色意味着这是您的行为,因此将侦听器置于活动中,然后更改视图的颜色,我不知道这是否是您问题的答案,但您可以尝试在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>
您还可以尝试为主布局提供Id,并通过基本操作和检索更改其背景。例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hello"
只需在相应活动的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(…)之后添加这一行
第三种方法
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));
将背景色设置为根视图
<?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>
我只想加上我的2美分。
我有同样的目标(改变.java类的背景色)。但上述方法对我都不起作用
问题是,我在layout.xml文件中使用android:background=“@color/colorGray”
:
试试这个:
您应该使用xml编写,其中后台
android:background="@drawable/imgname"
看看我的答案,我有一个对话框,打开了一个文本框。我想在单击“确定”时更改颜色。我尝试了这段代码,但没有成功。是因为我的视图现在在对话框界面上,而不是主要活动本身吗?我想出来了。如果有人处于类似的情况,只需在函数中执行以下操作:LinearLayout main=(LinearLayout)findViewById(R.id.myScreenMain);main.setBackgroundColor(颜色)@纳文:对不起,我没有看到你上面的评论。如果你想引起某人的注意,那么就用@
开始你的评论,然后用stackoverflow用户名,就像我在评论中为你做的那样。然后它将显示在用户的收件箱中。很高兴你找到了解决问题的方法。@Squonk:非常感谢你的帮助。我会记住:)即使这没有回答老年退休金计划的问题,它也回答了我的问题+1这个渐变背景建议太棒了。但愿我能给你更多+1。
<?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);
android:background="@drawable/imgname"