Dialog自定义布局的实现


       每次想要自定义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();
        }
    });
}
 好了 到此就结束了   增加或减少对话框控件的话  只需要修改布局即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值