1public class CustomDilaog { 2 3 private Context context; 4 private Dialog dialog; 5 6 public CustomDilaog(Context context) { 7 8 this.context = context; 9 dialog = new Dialog(context); 10 } 11 12 public void show() { 13 14 View view = LayoutInflater.from(context).inflate(R.layout.custom, null); 15 dialog.setContentView(view); 16 dialog.setTitle("自定义对话框"); 17 ImageView imageView = (ImageView) view.findViewById(R.id.image); 18 imageView.setImageResource(R.drawable.a1); 19 dialog.show(); 20 } 21} 22 23<?xml version="1.0" encoding="utf-8"?> 24<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:orientation="horizontal" 28 android:padding="10dp" > 29 30 <ImageView 31 android:id="@+id/image" 32 android:layout_width="wrap_content" 33 android:layout_height="fill_parent" 34 android:layout_marginRight="10dp" /> 35 36 <TextView 37 android:id="@+id/text" 38 android:layout_width="wrap_content" 39 android:layout_height="fill_parent" 40 android:text="自定义对话框内容" 41 android:textColor="#000000" /> 42 43</LinearLayout> 44 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 button = (Button) findViewById(R.id.button); 50 button.setOnClickListener(new OnClickListener() { 51 52 @Override 53 public void onClick(View v) { 54 // TODO Auto-generated method stub 55 CustomDilaog dilaog = new CustomDilaog(MainActivity.this); 56 dilaog.show(); 57 } 58 }); 59 }
Android 自定义对话框
Stella981
2021-10-11
982 0 0
点赞
收藏
评论区
加载中...
