加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在Activity中动态设置TextView的属性

(2013-01-26 19:35:31)
标签:

android于振北

于振北

it

分类: Android
package yu.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TextViewJava extends Activity {
private LinearLayout mLayout;
private TextView mTextView;
private RelativeLayout mLayout2;
private TextView mTextView2;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建一个线性布局
mLayout = new LinearLayout(this);
// 接着创建一个TextView
mTextView = new TextView(this);

// 第一个参数为宽的设置,第二个参数为高的设置。
mTextView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// 设置mTextView的文字
mTextView.setText("这是我的TextView");
// 设置字体大小
mTextView.setTextSize(20);
// 设置背景
mTextView.setBackgroundColor(Color.BLUE);
// 设置字体颜色
mTextView.setTextColor(Color.RED);
//设置居中
mTextView.setGravity(Gravity.CENTER);
//
mTextView.setPadding(1, 0, 0, 0);//left, top, right, bottom

// 将TextView添加到Linearlayout中去
mLayout.addView(mTextView);
// 创建一个线性布局
mLayout2 = new RelativeLayout(this);
// 接着创建一个TextView
mTextView2 = new TextView(this);

// 第一个参数为宽的设置,第二个参数为高的设置。
mTextView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// 设置mTextView的文字
mTextView2.setText("这是我的TextView");
// 设置字体大小
mTextView2.setTextSize(20);
// 设置背景
mTextView2.setBackgroundColor(Color.BLUE);
// 设置字体颜色
mTextView2.setTextColor(Color.RED);
// 设置居中
mTextView2.setGravity(Gravity.CENTER);
//相对位置
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mTextView2.getLayoutParams();
params.setMargins(1, 0, 0, 0);// 通过自定义坐标来放置你的控件left, top, right, bottom
mTextView .setLayoutParams(params);// 
// 将TextView添加到RelativeLayout中去
mLayout2.addView(mTextView2);
// 展现这个线性布局
setContentView(mLayout);
setContentView(mLayout2);

}
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有