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

来源:互联网 发布:淘宝买家可以延长收货 编辑:程序博客网 时间:2024/06/13 06:47

打开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{@Overrideprotected 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>

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


0 0