在代码当中控制UI界面

在代码中控制UI界面

    虽然Android推荐使用XML布局文件来控制UI界面,但如果开发者愿意,Android

许开发者像开发Swing应用一样,完全抛弃XML布局文件,完全在Java代码中控制UI

面。如果希望在代码中控制UI界面,那么所有的UI组件都将通过new关键字创建出来,然后以合适的方式“搭建”在一起即可。

下面将试图开发一个完全用代码控制UI界面的Android应用。由于该应用完全采用代码来控制UI界面,因此可以完全抛弃XML布局文件。

下面是通过代码控制UI界面的代码。

package com.example.codeview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class CodeView extends Activity 
{
	//当第一次创建该Activity时回调该方法
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		//调用父类的构造方法 这是继承的特性所要求的  并且这个回调函数其实跟构造方法很类似  在
		//view类的继承类当中  通过Context的参数类型  调用父类的构造函数
		super.onCreate(savedInstanceState);
		
		//创建一个线性布局管理器
		LinearLayout layout = new LinearLayout(this);
		
		//设置该Activity显示layout
		super.setContentView(layout);
		//参数是int类型的 其实是个enum
		layout.setOrientation(LinearLayout.VERTICAL);
		//创建一个TextView
		final TextView show = new TextView(this);
		//创建一个按钮  起码这个函数式Button自己的
		Button bn = new Button(this);
		bn.setText(R.string.hello_world);
		bn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
		//向Layout容器中添加TextView
		layout.addView(show);
		//向Layout容器中添加按钮
		layout.addView(bn);
        //为按钮绑定一个事件监听器
		bn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) 
			{
				show.setText("Hello , Android , "
					+ new java.util.Date());
			}
		});
	}
}

相关方法:

android.widget.LinearLayout.LinearLayout(Context context)

public LinearLayout (Context context)

Added in API level 1

void android.app.Activity.setContentView(View view)

public void setContentView (View view)

Added in API level 1

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.

Parameters

view

The desired content to display.

See Also

· setContentView(int)

· setContentView(android.view.View, android.view.ViewGroup.LayoutParams)

void android.widget.LinearLayout.setOrientation(int orientation)

public void setOrientation (int orientation)

Added in API level 1

Should the layout be a column or a row.

Related XML Attributes

· android:orientation

Parameters

orientation

Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL.

android.widget.TextView.TextView(Context context)

public TextView (Context context)

Added in API level 1

android.widget.Button.Button(Context context)

public Button (Context context)

Added in API level 1

void android.widget.TextView.setText(int resid)

public final void setText (int resid)

Added in API level 1

看的出来  Button确实是继承与TextView的  setText(int resid )本来是TextView的方法,Button是可以直接使用的

void android.view.View.setLayoutParams(LayoutParams params)

public void setLayoutParams (ViewGroup.LayoutParams params)

Added in API level 1

Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

Parameters

params

The layout parameters for this view, cannot be null

看的出来他们呢又都是继承于View类的

void android.view.View.setOnClickListener(OnClickListener l)

public void setOnClickListener (View.OnClickListener l)

Added in API level 1

Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.

Parameters

l

The callback that will run

See Also

· setClickable(boolean)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值