android Activity.this.getLayoutInflater()::動態載入佈局檔案,實現彈窗效
阿新 • • 發佈:2019-02-18
activity類的拓展方法1:getLayoutInflater():將layout的xml佈局檔案例項化為View類物件,實現動態載入佈局
MainActivity.java
package com.example.dynamiclayout; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; 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); Button btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(listener); } private View.OnClickListener listener=new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder=new Builder(MainActivity.this); LayoutInflater inflater=MainActivity.this.getLayoutInflater(); View layout=inflater.inflate(R.layout.login_view, null); builder.setIcon(R.drawable.ic_launcher) .setTitle("使用者登入") .setView(layout) .setPositiveButton("登陸", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //單機登陸按鈕後的業務邏輯 } }); builder.create().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; } }
layout:
1: activity_main.xml
<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" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:text="登陸" /> </RelativeLayout>
2:login_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:text="使用者名稱:" android:layout_width="90dp" android:layout_height="wrap_content" android:textSize="20sp"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/user"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:text="密碼" android:textSize="20sp"/> <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:password="true"/> </LinearLayout> </LinearLayout>
2)value/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">dynamiclayout</string>
<string name="action_settings">Settings</string>
<string name="hello_world">動態載入佈局檔案,實現彈窗效果</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynamiclayout"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.dynamiclayout.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>