在网上找了很多关于dialog的自定义样式的问题,还有很多人写得比较复杂,需要改动style什么的,或者是自定义dialog搞得很复杂,我最后还是找到了方法来实现。参考原文地址:http://jy0329.blog.163.com/blog/static/14746600220132204035120/
下面是我的dialog布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_margin="50dp">
<RelativeLayout
android:id="@+id/rl_dialog_content"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="@drawable/alertdialog_bg">
<TextView
android:id="@+id/dialog_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="现在就打电话给客服:arjinmc"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:maxLines="5"
android:gravity="center"
/>
<LinearLayout
android:id="@+id/ll_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_margin="30dp">
<Button
android:id="@+id/dialog_cancel"
android:text="@string/alert_cancel"
android:background="@drawable/btn_long_white"
style="@style/dialog_button"
android:layout_weight="1"
android:textColor="@color/tabs_font"
/>
<Button
android:id="@+id/dialog_ok"
android:text="@string/alert_ok"
android:background="@drawable/btn_long_red"
style="@style/dialog_button"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</RelativeLayout>
<ImageButton
android:id="@+id/dialog_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/alertdialog_close"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
/>
</RelativeLayout>
//布局文件转换为view对象
LayoutInflater inflaterDl = LayoutInflater.from(this);
RelativeLayout layout = (RelativeLayout)inflaterDl.inflate(R.layout.layout_dialog, null );
//对话框
final Dialog dialog = new AlertDialog.Builder(SettingActivity.this).create();
dialog.show();
dialog.getWindow().setContentView(layout);
//取消按钮
Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
}
});
//确定按钮
Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
}
});
//关闭按钮
ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
btnClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});