自定义对话框

23 篇文章 0 订阅
18 篇文章 0 订阅
在网上找了很多关于dialog的自定义样式的问题,还有很多人写得比较复杂,需要改动style什么的,或者是自定义dialog搞得很复杂,我最后还是找到了方法来实现。参考原文地址:http://jy0329.blog.163.com/blog/static/14746600220132204035120/
 
下面是我的dialog布局xml文件:
<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>


效果如图:
【Android】自定义dialog的布局样式


在代码中需要这项写就可以了:
//布局文件转换为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();          
           }
         });


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值