android 自定义带关闭按钮的dialog
16lz
2021-01-23
android 自定义带关闭按钮的dialog 在android的开发过程中,使用android常用的那几种dialog样式是远远不够的,不符合设计给出的设计图,尤其是右上角带有关闭按钮的dialog,之前一直都没有太在意这方面的,但是在实际的项目中,遇到了,上网查了一些资料,但是很少,而且觉得有点复杂,因此按照自己的理解实现了,效果图如下:
主要是在自定义的dialog.xml布局文件上,多的不说了,直接上代码吧:
1、dialog.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dip"
android:background="#ffffff">
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文字描述"
android:layout_margin="10dip"/>
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="0.25dp"
android:background="#cccccc"/>
android:layout_width="match_parent"
android:layout_height="150dip" >
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ratingBar1"
android:layout_centerHorizontal="true"
android:text="这是进度条" />
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/close"
android:layout_gravity="top|right" />
2、style样式
3、自定义的dialog类MyDialog.xml
package com.lly.cn;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MyDialog extends Dialog{
ImageView img_back;
Context context;
public MyDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog);
initview();
}
private void initview() {
// TODO Auto-generated method stub
img_back=(ImageView) findViewById(R.id.close);
img_back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyDialog.this.dismiss();
}
});
}
}
4、自定义dialog的使用
只要在你想用的地方加上这几句就可以 mydialog=new MyDialog(MainActivity.this,R.style.MyDialog);
mydialog.show();
mydialog.setCancelable(false); //点击dialog以外的地方dialog不消失,可根据自己的需要来改咯
这样就可以完成自定义带关闭按钮的dialog了,希望对你有用!!
源码下载
主要是在自定义的dialog.xml布局文件上,多的不说了,直接上代码吧:
1、dialog.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_height="wrap_content"
android:background="@android:color/transparent">
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dip"
android:background="#ffffff">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文字描述"
android:layout_margin="10dip"/>
android:layout_width="fill_parent"
android:layout_height="0.25dp"
android:background="#cccccc"/>
android:layout_height="150dip" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ratingBar1"
android:layout_centerHorizontal="true"
android:text="这是进度条" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/close"
android:layout_gravity="top|right" />
2、style样式
3、自定义的dialog类MyDialog.xml
package com.lly.cn;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MyDialog extends Dialog{
ImageView img_back;
Context context;
public MyDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog);
initview();
}
private void initview() {
// TODO Auto-generated method stub
img_back=(ImageView) findViewById(R.id.close);
img_back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyDialog.this.dismiss();
}
});
}
}
4、自定义dialog的使用
只要在你想用的地方加上这几句就可以 mydialog=new MyDialog(MainActivity.this,R.style.MyDialog);
mydialog.show();
mydialog.setCancelable(false); //点击dialog以外的地方dialog不消失,可根据自己的需要来改咯
这样就可以完成自定义带关闭按钮的dialog了,希望对你有用!!
源码下载
更多相关文章
- Android自定义控件——仿ios开关按钮
- Android -- Button(按钮)的几种监听方式
- android中listview的一些样式设置,自定义ListView行间的分割线
- Android 样式系统 | 主题背景和样式
- android设置控件样式(边框颜色,圆角)和图片样式(圆角)
- android上多样式文本的使用
- android 按钮的四种点击事件
- Android - 保持在底部的按钮栏,上面是滚动的ScrollView