开始学习android,争取每天练习一些实例,记下来,以后查起来也方便。这些实例有的是教材上的,还有网上的,自己重做一遍,希望有所帮助。
今天学习LayoutInflater的用法
LayoutInflater 方法主要是为一些界面选择布局.
以下是代码:
package u500wan.esun.com;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
// 创建监听
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
AlertDialog dialog ;
AlertDialog.Builder builder;
Context mContext = MainActivity.this;
//创建弹出窗口
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// 关键一步
View dialogLayout = inflater.inflate(R.layout.new_dialog, null);
ImageView imageView = (ImageView)dialogLayout.findViewById(R.id.imageView);
TextView textView = (TextView)dialogLayout.findViewById(R.id.text);
imageView.setImageResource(R.drawable.icon);
textView.setText("welcome!");
builder = new AlertDialog.Builder(mContext);
builder.setView(dialogLayout);
dialog = builder.create();
dialog.show();
}
});
}
}