Android学习实践:4.让Activity全屏及改变文字和背景颜色

本文详细介绍了如何使用XML和Java代码修改Android应用的布局结构和界面颜色,包括隐藏标题栏、设置全屏模式及改变View对象颜色的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开res/layout/activity_my.xml,修改改为如下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     
    <TextView
        android:id="@+id/text1"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />
     
</LinearLayout>

以上在LinearLayout中加入了andoird:id=@+id/layout1,设定其id,在TextView中加入了android:textColor="#FFFFFF"设定其文本颜色为白色。


打开MyActivity.java,修改为如下内容:

package maxwoods.demo1;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MyActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{  
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);		
		setContentView(R.layout.activity_my);
		View v=findViewById(R.id.layout1);
		v.setBackgroundColor(Color.RED);
	}
}

注意加入的代码:

1. requestWindowFeature(Window.FEATURE_NO_TITLE);  用来隐藏标题栏

2. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN); 用来设定为全屏

3. v.setBackgroundColor用来改变View对象的颜色,这里设为了Color.RED红色


当然,也可以不通过代码,直接修改activity_my.xml,在LinearLayout中加入属性android:background来实现同样的效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#FF0000">
     
    <TextView
        android:id="@+id/text1"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />
     
</LinearLayout>

运行后,点击“打开另一个”按钮,如图所示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

螃蟹就是横着走

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值