android创建自定义对话框

创建如下自定义对话框:

JAVA代码

LayoutInflater li = LayoutInflater.from(TagActivity. this);  //NOTE
final View TestView = li.inflate(R.layout.tag_dialog, null);
dialog =  new Dialog(TagActivity. this, R.style.dialog);
dialog.setContentView(TestView);
dialog.setCancelable( true);
dialog.show();

R.layout.tag_dialog如下

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  android:layout_width= "match_parent"  android:layout_height= "match_parent"  android:orientation= "vertical" >
   <LinearLayout android:layout_height= "wrap_content"  android:layout_width= "fill_parent" >
     <TextView android:id= "@+id/title"  android:layout_height= "wrap_content"  android:layout_width= "fill_parent"  android:gravity= "center"  android:textColor= "@color/white"  android:paddingTop= "15dp" ></TextView>
   </LinearLayout>
   <LinearLayout android:layout_width= "200dp"  android:layout_height= "200dp"  android:orientation= "vertical"  android:paddingLeft= "10dp"  android:paddingRight= "10dp"  android:paddingTop= "10dp" >
     <EditText android:id= "@+id/name"  android:layout_width= "fill_parent"  android:layout_height= "wrap_content"  android:background= "@color/transparent"  android:textColor= "@color/white"  android:hint= "输入消息内容"  android:textColorHint= "@color/white" ></EditText>
   </LinearLayout>
   <LinearLayout android:layout_width= "fill_parent"  android:layout_height= "wrap_content"  android:orientation= "horizontal"  android:layout_marginBottom= "10dp" >
     <RelativeLayout android:layout_width= "fill_parent"  android:layout_height= "wrap_content"  android:layout_weight= "1" >
       <ImageButton android:id= "@+id/sure"  android:layout_width= "wrap_content"  android:layout_height= "wrap_content"  android:src= "@drawable/sure"  android:background= "@drawable/btn_click_background"  android:layout_centerHorizontal= "true" ></ImageButton>
     </RelativeLayout>
     <RelativeLayout android:layout_width= "fill_parent"  android:layout_height= "wrap_content"  android:layout_weight= "1" >
       <ImageButton android:id= "@+id/cancel"  android:layout_width= "wrap_content"  android:layout_height= "wrap_content"  android:src= "@drawable/cancel"  android:background= "@drawable/btn_click_background"  android:layout_centerHorizontal= "true" ></ImageButton>
     </RelativeLayout>
   </LinearLayout>
</LinearLayout>

R.style.dialog文件如下

<!-- 下面是对话框对应的style -->
<style name= "dialog"  parent= "@android:style/Theme.Dialog" >
   <item name= "android:windowFrame" >@null </item>
   <item name= "android:windowIsFloating" >true </item>
   <item name= "android:windowIsTranslucent" >true </item>
   <item name= "android:windowNoTitle" >true </item>
   <!--<item name="android:background">@android:color/transparent</item>-->
   <item name= "android:background" >@android:color /transparent </item>
   <item name= "android:windowBackground" >@drawable /dialog_bg </item>
   <item name= "android:backgroundDimEnabled" >true </item>
   <item name= "android:backgroundDimAmount" >0.6 </item>
   <item name= "android:windowFullscreen" >true </item>
</style>
原文地址:https://www.cnblogs.com/dyllove98/p/3177731.html