Android开发之如何手写代码进行页面布局

本文介绍如何使用LinearLayout在Android应用中创建基本界面。通过实例演示了如何定义布局参数、添加TextView并设置其属性。
首先,创建线性布局对象
 LinearLayoutlayout = new LinearLayout(this);//为本Activity创建一个线性布局对象
//并且设置它的属性 android:layout_width与 android:layout_height都为 FILL_PARENT
LinearLayout.LayoutParamslayoutParams =  new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
然后,为本Activity创建一个TextView,代码如下
 TextViewtextView = new TextView(this);
然后设置TextView的属性
textView.setText(R.string.hello);
textView.setId(34);
对于布局方面的属性这样来设置
 LinearLayout.LayoutParams textviewParams = newLinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
接着在线性布局对象中加入这个TextView
layout.addView(textView,textviewParams);//加入的同时,也就设置了TextView相对于布局对象的布局属性 android:layout_width与 android:layout_height
最后一步,设置本Activity的顶级界面为线性布局
setContentView(layout,layoutParams);//同时也就设置了布局对象的android:layout_width与 android:layout_height
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值