您的位置:首页 > 移动开发 > Android开发

android基础--通过编码实现软件界面

2011-10-18 17:46 507 查看
public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);//创建一个线性布局

layout.setOrientation(LinearLayout.VERTICAL);//设置布局的方向

LinearLayout.LayoutParams params

= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,

ViewGroup.LayoutParams.FILL_PARENT); //为布局设置参数,长度宽度填充父元素

LinearLayout.LayoutParams params2

= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//为布局设置参数,长度宽度填充父元素

TextView textView = new TextView(this);

textView.setText(R.string.hello);

layout.addView(textView,params2);

//上面是通过代码实现界面,下面代码时通过xml文件生成界面

LayoutInflater inflater

= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); //布局填充服务

View view = inflater.inflate(R.layout.part, null);//用part.xml生成一个view对象

layout.addView(view);

setContentView(layout,params);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: