自定义dialog显示详细资料以及出现的布局问题解决

as和小米pad2测试 自定义dialog,显示如下

dialog顶部有一块空白,原因是在自定义dialog时,extends Dialog,Dialog有一个默认title,就是图片中的空白部分,想要去掉,改成 extends AlertDialog,最后图片如下:

现在贴出自定dialog的源码:

(1)dialog_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@android:color/white">


    <ImageView
        android:id="@+id/img"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"/>
    <!-- 姓名-->
    <LinearLayout
        android:id="@+id/layout_name"
        android:layout_width="match_parent"
        android:layout_height="23dp"
        android:layout_below="@+id/img"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:gravity="right|center_vertical"
            android:text="@string/name"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_gender"
        android:layout_width="match_parent"
        android:layout_height="23dp"
        android:layout_below="@+id/layout_name"
        android:layout_marginTop="12dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:gravity="right|center_vertical"
            android:text="@string/gender"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_cardId"
        android:layout_width="match_parent"
        android:layout_height="23dp"
        android:layout_below="@+id/layout_gender"
        android:layout_marginTop="12dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:gravity="right|center_vertical"
            android:text="@string/cardId"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>

        <TextView
            android:id="@+id/tv_cardId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_phone"
        android:layout_width="match_parent"
        android:layout_height="23dp"
        android:layout_below="@+id/layout_cardId"
        android:layout_marginTop="12dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:gravity="right|center_vertical"
            android:text="@string/phone"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>

        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_level"
        android:layout_width="match_parent"
        android:layout_height="23dp"
        android:layout_below="@+id/layout_phone"
        android:layout_marginTop="12dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:gravity="right|center_vertical"
            android:text="@string/level"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>

        <TextView
            android:id="@+id/tv_level"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_remark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_level"
        android:layout_marginTop="12dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="180dp"
            android:layout_height="23dp"
            android:gravity="right|center_vertical"
            android:text="@string/remark"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>

        <TextView
            android:id="@+id/tv_remark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/common_textSize_default14"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_remark"
        android:layout_marginTop="24dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_sure"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:background="@drawable/btn_signin_style"
            android:gravity="center"
            android:padding="5dp"
            android:text="@string/sure"
            android:textColor="@android:color/white"
            android:textSize="@dimen/common_textSize_default16"/>

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@drawable/btn_cancel_style"
            android:gravity="center"
            android:padding="5dp"
            android:text="@string/cancel"
            android:textColor="@color/logincolor"
            android:textSize="@dimen/common_textSize_default16"/>
    </LinearLayout>
</RelativeLayout>

布局中的@color @string @drawable就不贴出了,样式,字段引用自己替换就能用,不难

(2)自定义Dialog:

DetailRecordDialog.java:
public class DetailRecordDialog extends AlertDialog {//AlertDialog和Dialog两种继承

    private final String TAG = "SJY";
    //变量

    private Context context;
    private DetailModel model;

    private ImageLoader imgLoader;
    private DisplayImageOptions imgOptions;

    private ClickListenerInterface clickListenerInterface;
    //控件
    private ImageView img;
    private TextView tv_name;
    private TextView tv_gender;
    private TextView tv_cardId;
    private TextView tv_phone;
    private TextView tv_level;
    private TextView tv_remark;

    private Button btn_sure;
    private Button btn_cancel;

    public interface ClickListenerInterface {
        public void forSure();

        public void forCancel();
    }

    public DetailRecordDialog(Context context, DetailModel model) {
        super(context);
        this.context = context;
        this.model = model;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        setValue();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_detail, null);
        setContentView(view);

        imgLoader = ImageLoader.getInstance();
        imgLoader.init(ImageLoaderConfiguration.createDefault(context));
        imgOptions = ImageLoadingConfig.generateDisplayImageOptions(R.mipmap.default_photo);
        //初始化
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_gender = (TextView) view.findViewById(R.id.tv_gender);
        tv_cardId = (TextView) view.findViewById(R.id.tv_cardId);
        tv_phone = (TextView) view.findViewById(R.id.tv_phone);
        tv_level = (TextView) view.findViewById(R.id.tv_level);
        tv_remark = (TextView) view.findViewById(R.id.tv_remark);
        img = (ImageView) view.findViewById(R.id.img);

        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);

        //
    }

    //显示
    public void setValue() {

        //参数
        tv_name.setText(model.getClientName());
        if ((model.getClientGender()).contains("1")) {
            tv_gender.setText("男");
        } else if ((model.getClientGender()).contains("2")) {
            tv_gender.setText("女");

        } else {
            tv_gender.setText(model.getClientGender());
        }

        String cardNum = model.getIDCardNo();
        tv_cardId.setText(TextUtils.isEmpty(cardNum) ? "无" : cardNum);
        tv_phone.setText(model.getClientPhone());
        tv_level.setText(AppCommonUtils.getTransLevel(model.getClientLevel()));
        tv_remark.setText(model.getRemark());
        String url = WebUrl.getURL() + model.getImgPath();
        Log.d(TAG, "setValue: 图片路径:" + url);
        imgLoader.displayImage(url, img, imgOptions);

        btn_cancel.setOnClickListener(new ClickListener());
        btn_sure.setOnClickListener(new ClickListener());

        //弹窗大小,代码设置
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        layoutParams.width = DpUtils.dp2px(context, 430); // 宽430dp
        layoutParams.height = DpUtils.dp2px(context, 525); // 高525dp
        dialogWindow.setAttributes(layoutParams);

    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.btn_sure:
                    clickListenerInterface.forSure();
                    break;
                case R.id.btn_cancel:
                    clickListenerInterface.forCancel();
                    break;
            }
        }

    }

    ;
}

*显示图片我是用了ImageLoader,其实更建议使用Glide,比ImageLoader使用方便。不用图片的,这段代码可以删除。对应的
ImageLoadingConfig.java类我就不贴了。
*相比较设置自定义弹窗,更推荐使用代码,我使用的只设置了宽高,公司UI的设置是宽430dp,高525dp:
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        layoutParams.width = DpUtils.dp2px(context, 430); // 宽430dp
        layoutParams.height = DpUtils.dp2px(context, 525); // 高525dp
        dialogWindow.setAttributes(layoutParams);




*代码中
DpUtils.dp2px(context, 430):
public static int dp2px(Context context, float dipValue) {
        //
        final float density = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * density + 0.5f);
    }



*
DetailModel 是保存数据的实体,来自后台:
public class DetailModel implements Serializable {
    private static final long serialVersionUID = 1L;
    private String ClientID;
    private String ClientName;
    private String ClientGender;
    private String IDCardNo;
    private String ClientLevel;
    private String Remark;
    private String ActiveFlag;
    private String CreateTime;
    private String ImgPath;
    private String clientPhone;

    public String getClientPhone() {
        return clientPhone;
    }

    public void setClientPhone(String clientPhone) {
        this.clientPhone = clientPhone;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getClientID() {
        return ClientID;
    }

    public void setClientID(String clientID) {
        ClientID = clientID;
    }

    public String getClientName() {
        return ClientName;
    }

    public void setClientName(String clientName) {
        ClientName = clientName;
    }

    public String getClientGender() {
        return ClientGender;
    }

    public void setClientGender(String clientGender) {
        ClientGender = clientGender;
    }

    public String getIDCardNo() {
        return IDCardNo;
    }

    public void setIDCardNo(String IDCardNo) {
        this.IDCardNo = IDCardNo;
    }

    public String getClientLevel() {
        return ClientLevel;
    }

    public void setClientLevel(String clientLevel) {
        ClientLevel = clientLevel;
    }

    public String getRemark() {
        return Remark;
    }

    public void setRemark(String remark) {
        Remark = remark;
    }

    public String getActiveFlag() {
        return ActiveFlag;
    }

    public void setActiveFlag(String activeFlag) {
        ActiveFlag = activeFlag;
    }

    public String getCreateTime() {
        return CreateTime;
    }

    public void setCreateTime(String createTime) {
        CreateTime = createTime;
    }

    public String getImgPath() {
        return ImgPath;
    }

    public void setImgPath(String imgPath) {
        ImgPath = imgPath;
    }
}





在activity/fragment中使用,在listView的onItemClickListener中:

 final DetailRecordDialog dialog = new DetailRecordDialog(getActivity(), model);
        dialog.show();
        dialog.setClicklistener(new DetailRecordDialog.ClickListenerInterface() {
            @Override
            public void forSure() {
                //修改
                Intent intent = new Intent(getActivity(), VipUpdateActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("DetailModel", model);
                intent.putExtras(bundle);
                startActivity(intent);
                dialog.dismiss();
            }

            @Override
            public void forCancel() {
                dialog.dismiss();
            }
        });

至此,实践就可以使用了。

参考:(1)http://www.cnblogs.com/lwbqqyumidi/p/3645561.html

(2)http://www.th7.cn/Program/Android/201612/1043845.shtml



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值