Android之自定义对话框AlertDialog.Builder+getLayoutInflater().inflate
2016-09-02 17:31
477 查看
activity文件:AccountDetailActivity.java
public class AccountDetailActivity extends BaseActivity{ private View configLayout; configLayout = view.findViewById(R.id.configDetailLayout); configLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub final AlertDialog dialog = new AlertDialog.Builder(AccountDetailActivity.this).create();//创建一个AlertDialog对象 View view = getLayoutInflater().inflate(R.layout.dialog_confirm, null);//自定义布局 TextView message = (TextView)view.findViewById(R.id.message); message.setText("是否退出?"); View ok = view.findViewById(R.id.ok); ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { app.logout(); app.setVersion(app.getVersion() + 1); AccountDetailActivity.this.finish(); }} ); View cancel = view.findViewById(R.id.cancel); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //nothing to do dialog.dismiss(); }} ); dialog.setView(view, 0, 0, 0, 0);//把自定义的布局设置到dialog中,注意,布局设置一定要在show之前。从第二个参数分别填充内容与边框之间左、上、右、下、的像素 dialog.show();//一定要先show出来再设置dialog的参数,不然就不会改变dialog的大小了 } }); }
整体布局文件:activity_account_detail.xml
<RelativeLayout android:id="@+id/configDetailLayout" android:layout_width="match_parent" android:layout_height="50dip" android:background="@drawable/gray_selector" android:clickable="true" android:paddingLeft="20dip" android:paddingRight="20dip" > <TextView style="@style/text_s32_1c1c1c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:text="退出登录" /> </RelativeLayout>
自定义布局文件:dialog_confirm.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dip" android:layout_height="150dip" android:background="@color/white" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="100dip"> <TextView android:id="@+id/message" style="@style/text_s28_1c1c1c" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="是否要删除该记录?"/> </RelativeLayout> <RelativeLayout android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="50dip"> <View android:layout_width="fill_parent" android:layout_height="1px" android:layout_alignParentTop="true" android:background="@color/cdddddd"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true"> <RelativeLayout android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="49dip" android:background="@drawable/white_selector" android:layout_weight="1" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"> <TextView android:id="@+id/cancel_text" style="@style/text_s28_1c1c1c" android:layout_gravity="center_vertical" android:text="取消"/> </LinearLayout> </RelativeLayout> <View android:layout_width="1dip" android:layout_height="25dip" android:layout_gravity="center" android:background="@color/cdddddd"/> <RelativeLayout android:id="@+id/ok" android:layout_width="match_parent" android:layout_height="49dip" android:background="@drawable/white_selector" android:layout_weight="1" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"> <TextView android:id="@+id/ok_text" style="@style/text_s28_1c1c1c" android:layout_gravity="center_vertical" android:text="确定"/> </LinearLayout> </RelativeLayout> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dip" android:layout_alignParentBottom="true" android:background="@color/cdddddd"/> </RelativeLayout> </LinearLayout>
相关文章推荐
- Android开发之AlertDialog 关于重写onCreatDialog自定义对话框的问题 问题出自于《疯狂Android讲义》这本书
- Android listView,自定义Adapter,另The constructor AlertDialog.Builder(MyAdapter) is undefined问题
- android-如何关闭AlertDialog.Builder对话框
- android-如何关闭AlertDialog.Builder对话框
- Android:AlertDialog.Builder创建常见对话框
- Android控件之AlertDialog-自定义对话框
- android-如何关闭AlertDialog.Builder对话框
- android-如何关闭AlertDialog.Builder对话框
- android AlertBuilder创建的dialog,使用自定义样式
- android Alertdialog.Builder 创建对话框的七种方式
- android-如何关闭AlertDialog.Builder对话框
- android-如何关闭AlertDialog.Builder对话框
- android 简单列表对话框(AlertDialog.Builder().setItems())
- android AlertDialog.Builder 设置对话框不消失
- android基础学习(6)-------AlertDialog.Builder提示对话框
- (转)android中的alertdialog及LayoutInflater
- Android自定义UI陷阱:LayoutInflater.from().inflate()一定不能工作在父类或虚类里
- 使用AlertDialog.Builder自定义对话框
- 完全自定义Android对话框AlertDialog的实现
- AlertDialog.Builder弹出自定义Layout窗口