每次想要自定义dialog的布局时,总是记得迷迷糊糊不清楚,今天专门花些时间去整理并自己实验了一次,成功之后将其总结过来,以便以后用时方便,若能帮助其他人,再好不过了。。
首先是准备工作 定义好一个布局,展示你所想展示的布局样式
--这是我的布局
<?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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择省" android:textColor="#ffffff" android:gravity="center" android:background="#6fff0000"></TextView> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#00ff00"></View> <ListView android:id="@+id/select_dialog_listviewProvince" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
其次就是在需要引用对话框的地方引用了
比如 最简的 点击按钮 弹出对话框
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_home" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jianing.practice.HomeActivity"> <Button android:id="@+id/showDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dialog"/> </RelativeLayout>
最后在想要弹出对话框的地方引用对话框
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); showDialog = (Button) findViewById(R.id.showDialog); showDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //定义Dialog Dialog dialog = new Dialog(HomeActivity.this); //此行是设置默认带的标题蓝线 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //定义dialog布局 View view = View.inflate(HomeActivity.this,R.layout.dialog_dialog,null); //将布局添加到dialog dialog.setContentView(view); TextView textViewTitle = (TextView) dialog.findViewById(R.id.textTitle); ListView listview = (ListView) dialog.findViewById(R.id.select_dialog_listviewProvince); dialog.show(); } }); }好了 到此就结束了 增加或减少对话框控件的话 只需要修改布局即可