今天给大家讲讲自定义对话框,前面一篇是利用系统的对话框,局限性太大,当我们想自己定义视图的时候,就不能利用系统函数了,就需要我们这里的自定义对话框了。
1.先看效果图,自定义对话框,四个ImageView排列
先看布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="5dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageview1"
android:src="@drawable/ym1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp" />
<ImageView
android:id="@+id/imageview2"
android:text="你好"
android:src="@drawable/ym2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp" />
<ImageView
android:id="@+id/imageview3"
android:src="@drawable/ym3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp" />
<ImageView
android:id="@+id/imageview4"
android:src="@drawable/ym4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp" />
LinearLayout>
然后自定义Dialog
public class CustomDialog extends Dialog {
public Context context;
public CustomDialog(Context context) {
super(context);
this.context = context;
}
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View inflate = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
setContentView(inflate);
}
}
主页面点击按钮弹出对话框
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(View view){
CustomDialog customDialog = new CustomDialog(this);
customDialog.show();
}
}
2.修改样式
我们可以看到在自定义的布局上方有一片白色的区域,之所以出现那部分,是因为我们没有定义对话框,系统采用默认的样式,那部分就是标题栏。要去掉的话,就要自定义样式。
我们需要在values下的style.xml文件里面定义样式,我们定义如下的样式:
android:windowFrame:界面对应的前景图片;
android:windowIsFloating:表示浮在屏幕上的,如果在这里使用了,整个layout就会在 屏幕中心,相当于浮在屏幕上,所以这个只适用于dialog
android:windowContentOverlay:表示标题栏的阴影部分的样式,使用图片或者颜色
android:windowNoTitle:标题栏是否隐藏,这就是我们上面显示的标题栏
android:windowBackground:设置对话框的背景,这里我们设置为白色。
接下来我们使用样式,有两种方式
第一种,在创建对象时使用样式:
CustomDialog customDialog = new CustomDialog(this,R.style.dialog);
第二种方法,就是我们在构造时,不让别人传样式,只让传进来Context,但在内部,我们利用Super(context,theme)来指定样式,代码如下:
public CustomDialog(Context context) {
super(context,R.style.dialog);
this.context = context;
}
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
this.context = context;
}
不管使用哪种方式,都能实现效果如下
这涉及到两个问题,传进来和传出去:
传进来:我们点击两个不同的按钮,然后把当前点击的按钮显示到对话框中。
传出去:点击对话框中的某一项,把当前点击的图片显示到主页面
先看效果图
1.传进来:
很简单,利用构造函数
private String str;
public CustomDialog(Context context,String str) {
super(context,R.style.dialog);
this.context = context;
this.str = str;
this.listener = listener;
}
然后在点击按钮的时候,传入参数即可
CustomDialog customDialog = new CustomDialog(this, "button1");
customDialog.show();
2.传出去:
这里利用回调函数,回调已经写了好多遍了,不啰嗦了,直接上代码
public class CustomDialog extends Dialog implements View.OnClickListener {
public Context context;
private TextView textView;
private String str;
private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private ImageView imageView4;
public OnImageSelectedListener listener;
public interface OnImageSelectedListener{
public void onImageSelected(int position);
}
public CustomDialog(Context context,String str) {
super(context,R.style.dialog);
this.context = context;
this.str = str;
}
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
this.context = context;
}
public void setOnImageSelectedListener(OnImageSelectedListener listener){
this.listener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View inflate = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
textView = (TextView) inflate.findViewById(R.id.textview);
imageView1 = (ImageView) inflate.findViewById(R.id.imageview1);
imageView2 = (ImageView)inflate. findViewById(R.id.imageview2);
imageView3 = (ImageView) inflate.findViewById(R.id.imageview3);
imageView4 = (ImageView) inflate.findViewById(R.id.imageview4);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
imageView4.setOnClickListener(this);
textView.setText(str);
setContentView(inflate);
}
@Override
public void onClick(View v) {
int drawableId = -1;
switch (v.getId()){
case R.id.imageview1:
drawableId = R.drawable.ym1;
break;
case R.id.imageview2:
drawableId = R.drawable.ym2;
break;
case R.id.imageview3:
drawableId = R.drawable.ym3;
break;
case R.id.imageview4:
drawableId = R.drawable.ym4;
break;
}
if(drawableId!=-1){
Toast.makeText(context,"drawid"+drawableId,Toast.LENGTH_SHORT).show();
listener.onImageSelected(drawableId);
}
dismiss();
}
}
public class MainActivity extends Activity implements CustomDialog.OnImageSelectedListener {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageview);
}
public void show1(View view){
CustomDialog customDialog = new CustomDialog(this, "button1");
customDialog.setOnImageSelectedListener(this);
customDialog.show();
}
public void show2(View view){
CustomDialog customDialog = new CustomDialog(this, "button2");
customDialog.setOnImageSelectedListener(this);
customDialog.show();
}
@Override
public void onImageSelected(int position) {
imageView.setImageResource(position);
}
}