安卓课本例子-02-使用Java控制UI界面

 1 package com.javaui;
 2 
 3 import android.graphics.Color;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.util.TypedValue;
 7 import android.view.Gravity;
 8 import android.view.ViewGroup;
 9 import android.widget.FrameLayout;
10 import android.widget.FrameLayout.LayoutParams;
11 import android.widget.TextView;
12 
13 public class MainActivity extends AppCompatActivity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         FrameLayout frameLayout = new FrameLayout(this);
19         // 设置Activity中显示frameLayout
20         setContentView(frameLayout);
21         // 添加Text1
22         TextView text1 = new TextView(this);
23         // 设置显示的文字
24         text1.setText("在Java代码中控制UI界面");
25         // 设置文字的大小
26         text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
27         text1.setTextColor(Color.rgb(1, 1, 1));
28         frameLayout.addView(text1);
29         // 添加text2
30         TextView text2 = new TextView(this);
31         // 设置text2显示的文字
32         text2.setText("单击进入游戏");
33         // 设置text2文字大小
34         text2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
35         // 设置text2文字颜色
36         text2.setTextColor(Color.rgb(1, 1, 1));
37         // 创建保存布局参数的对象
38         LayoutParams params = new LayoutParams(
39                 ViewGroup.LayoutParams.WRAP_CONTENT,
40                 ViewGroup.LayoutParams.WRAP_CONTENT);
41         // 设置居中显示
42         params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
43         // 设置布局参数
44         text2.setLayoutParams(params);
45         frameLayout.addView(text2);
46     }
47 }

书本中的实例代码, 运行成功

转载于:https://www.cnblogs.com/huangZ-H/p/10643717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值