android 模仿ios 对话框,android 仿ios 对话框已封装成工具类

对话框 在android中是一种很常见的交互提示用户的方式,但是很多产品狗都叫我们这些做android的仿ios,搞的我们android程序员很苦逼,凭什么效果老是仿ios,有没有一点情怀,不过ios在界面封装确实比android好很多,吐槽完毕,比如一种很常见的场景就是在没网的情况下 提示用户,看效果图:

98e767771c6e5ee4b4c7c2adc8e5da4f.png

在很多界面都要有提示,那么就自然而然想到了封装,而不至于在每个页面都重写一篇,话不多说直接上代码

CommonDialog.javapublic class CommonDialog extends Dialog{

private Context context;

private String title;

private ClickListenerInterface clickListenerInterface;

public CommonDialog(Context context) {

super(context, R.style.MyDialogStyle);

this.context = context;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

init();

}

private void init() {

LayoutInflater inflater =LayoutInflater.from(context);

View view =inflater.inflate(R.layout.dialog_common, null);

setContentView(view);

TextView tv_ok= (TextView) view.findViewById(R.id.tv_ok);

tv_ok.setOnClickListener(new clickListener());

Window dialogWindow = getWindow();

WindowManager.LayoutParams lp = dialogWindow.getAttributes();

DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用

lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6

dialogWindow.setAttributes(lp);

}

public interface ClickListenerInterface {

public void doConfirm();

}

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.tv_ok:

dismiss();

break;

}

}

};

}

dialog_common.xml

android:id="@+id/dialog_layout"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/login_bg"

android:gravity="center_horizontal"

android:orientation="vertical"

android:layout_marginBottom="10dp"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="1dp"

android:gravity="center_horizontal"

android:padding="10dp"

android:text="温馨提示"

android:textColor="@color/c282828"

android:textSize="18sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请求超时,请确定网络是否正常"

android:textSize="15sp"

android:textColor="#383838"

/>

android:layout_width="match_parent"

android:layout_height="0.4dp"

android:background="@color/line_color01"

android:layout_marginTop="10dp"

/>

android:id="@+id/tv_ok"

android:layout_width="fill_parent"

android:layout_height="30dp"

android:text="确定"

android:textSize="16sp"

android:layout_marginTop="10dp"

android:layout_marginBottom="10dp"

android:focusable="true"

android:clickable="true"

android:gravity="center"

android:textColor="#00B2EE"

/>

对话框样式 style

@android:color/transparent

@null

true

true

true

@null

@android:style/Animation.Dialog

true

使用:

final CommonDialog confirmDialog = new CommonDialog(getActivity());

confirmDialog.show();

confirmDialog.setClicklistener(new CommonDialog.ClickListenerInterface() {

@Override

public void doConfirm() {}

});

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/coderinchina/article/details/46998943

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值