android LayoutInflater小实例

  • 内容
  • 评论
  • 相关
package com.example.mytest;

import android.os.Binder;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Builder builder = new Builder(MainActivity.this);
		builder.setTitle("注册");
		
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.registerlayout, null);
		final AlertDialog dialog = builder.setView(view).create();
		
		Button btn_register = (Button) findViewById(R.id.register);
		btn_register.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.show();
			}
		});
	}

	@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;
	}

}

布局文件

<RelativeLayout 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:background="@drawable/bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <AutoCompleteTextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/userPassword"
        android:layout_toRightOf="@+id/textView2"
        android:ems="10"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/userPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView2"
        android:ems="10"
        android:hint="请输入密码"
        android:inputType="textPassword" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/userName"
        android:layout_marginTop="21dp"
        android:text="密    码:"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <Button
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/login"
        android:layout_alignBottom="@+id/login"
        android:layout_alignLeft="@+id/textView1"
        android:text="注册"
        android:background="@drawable/login_btn_click" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/userPassword"
        android:layout_below="@+id/userPassword"
        android:layout_marginTop="42dp"
        android:text="退出"
        android:background="@drawable/login_btn_click" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="50dp"
        android:text="用户名:"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:src="@drawable/student" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/exit"
        android:layout_alignBottom="@+id/exit"
        android:layout_alignRight="@+id/imageView1"
        android:background="@drawable/login_btn_click"
        android:text="登录" />

</RelativeLayout>

完整项目工程文件
http://pan.baidu.com/s/1c07qRja

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注