您的位置:首页 > 产品设计 > UI/UE

Android编程方式开发UI界面和XML文件与Java代码混合控制UI界面

2013-10-14 23:00 483 查看
编程方式开发UI界面

MainActivity.java

package com.ezwj.codeconsole;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//创建一个线性布局
LinearLayout layout = new LinearLayout(this);
//设置该Activity显示layout
super.setContentView(layout);
//设置该布局的排列方式
layout.setOrientation(LinearLayout.VERTICAL);
//创建一个TextView
final TextView show = new TextView(this);
//创建一个按钮
Button bn = new Button(this);
bn.setText(R.string.ok);
//设置按钮的宽高
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 arg0) {
//设置TextView
show.setText("Hello,Android," + new java.util.Date());
}});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
/**
*总结:
*1.从上面代码中可以看出所有的UI组件都是new出来的,然后程序使用
*LinearLayout容器来“盛装”这些UI组件,这样就组成了图形用户界面
*
*2.还可以看出无论创建哪种UI组件,都需要传入一个this参数,
*因为创建UI组件的时候传入一个Context参数,Context代表访问Android应用
*全局信息的API,让UI组件持有一个Context参数,可以让这些UI组件通过该
*Context参数来获取Android应用环境的全剧信息。
*
*弊端:
*1.不利于高层次的解耦
*2.通过new出UI组件,代码十分臃肿
*/


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">CodeConsole</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="ok">如题,这是OK</string>

</resources>


XML布局文件和Java代码混合控制UI界面
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root"
android:orientation="vertical"
>

</LinearLayout>


MainActivity.java

package com.zwj.mixedconsole;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

//定义一个访问图片的数组
int[] images = new int[]{R.drawable.java,R.drawable.ee,R.drawable.classic,R.drawable.ajax,R.drawable.xml};
int currentImg = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取LinearLayout布局容器
LinearLayout main = (LinearLayout)findViewById(R.id.root);
//程序创建ImageView组件
final ImageView image = new ImageView(this);
//将ImageView组件添加到LinearLayout布局容器中
main.addView(image);
//初始化时显示第一张图片
image.setImageResource(images[0]);
image.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//改变ImageView里显示的土坯
image.setImageResource(images[++currentImg % images.length]);

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

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