自定义AlertDialog的实现以及添加属性设置背景

自定义AlertDialog需要自己写的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_gravity="center_vertical"
    android:layout_margin="8dip"
     android:background="@android:color/transparent"
     style="@style/dialog_styles"
     >

    <RelativeLayout
        android:id="@+id/tip_txt_rela"
        android:layout_width="fill_parent"
        android:layout_height="35.0dip" >

        <TextView
            android:id="@+id/tip_txt"
            android:layout_width="wrap_content"
            android:layout_height="40.0dip"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:padding="2.0dip"
            android:text="请选择社交距离"
            android:textColor="#815F3D"
            
            android:textSize="14.0sp" />

    </RelativeLayout>

   
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:gravity="center_vertical"
          android:layout_below="@id/tip_txt_rela"
          >
          <Button
              android:id="@+id/one"
              android:layout_gravity="center"
                android:layout_width="300dp"
          android:layout_height="50dp"
          android:text="100米"
          android:background="#996600"
          android:textColor="#ffffff"
          android:layout_centerInParent="true"
              />
           <ImageView
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="@drawable/sidebar_list_pressed"/>
           <Button
               android:id="@+id/two"
                 android:layout_gravity="center"
                android:layout_width="300dp"
          android:layout_height="50dp"
          android:text="1000米"
          android:background="#996600"
          android:layout_centerInParent="true"
          android:textColor="#ffffff"
              />
            <ImageView
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="@drawable/sidebar_list_pressed"/>

         

               <Button
                   android:id="@+id/three"
                   android:layout_width="300dp"
                   android:layout_height="50dp"
                   android:background="#996600"
                   android:text="3000米"
                     android:layout_gravity="center"
                   android:textColor="#ffffff" />

               <ImageView
                   android:layout_width="200dp"
                   android:layout_height="wrap_content"
                   android:background="@drawable/sidebar_list_pressed" />

               <Button
                   android:id="@+id/four"
                   android:layout_width="300dp"
                   android:layout_height="50dp"
                   android:background="#996600"
                     android:layout_gravity="center"
                   android:text="全国"
                   android:textColor="#ffffff" />
           
</LinearLayout>

</RelativeLayout>

下面是我们的style

<style name="dialog_styles" parent="@android:style/Theme.Panel">

        <item name="android:windowFrame">@null</item><!--边框-->
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">false</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--无标题-->
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item><!--模糊-->

    </style>

下面再看我们的实现代码

dlg = new AlertDialog.Builder(context).create();
                
                dlg.setView(LayoutInflater.from(context).inflate(
                        R.layout.create_user_dialog, null));

                dlg.show();
                final Window window = dlg.getWindow();
                
                window.setContentView(R.layout.create_user_dialog);
                WindowManager.LayoutParams layparam = window.getAttributes();
                layparam.width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
                window.setAttributes(layparam);
               


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值