android 显示自定义视图对话框

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义视图对话框"
android:id="@+id/btnCustomViewDialog"
/>

</LinearLayout>

dialog_custom.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="center_horizontal"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading...."/>
</LinearLayout>

MainActivity.java:

this.findViewById(R.id.btnCustomViewDialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("自定义视图");
View view = View.inflate(MainActivity.this,R.layout.dialog_custom,null);

builder.setView(view);

builder.show();
}
});

原文地址:https://www.cnblogs.com/xiaoxiaomeng/p/5679815.html