用PopupWindow实现自定义Dailog
来源:互联网 发布:wampserver 64 mac 编辑:程序博客网 时间:2024/04/29 03:14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:orientation
=
"vertical"
android:background
=
"@drawable/shape_bg"
android:layout_margin
=
"10dp"
>
<
TextView
android:id
=
"@+id/CustomDlgTitle"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:textStyle
=
"bold"
android:textSize
=
"20sp"
android:layout_margin
=
"10dp"
android:gravity
=
"center"
/>
<
View
android:layout_width
=
"match_parent"
android:layout_height
=
"1dp"
android:background
=
"@android:color/darker_gray"
/>
<
LinearLayout
android:id
=
"@+id/CustomDlgContentView"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"vertical"
android:layout_margin
=
"5dp"
/>
<
TextView
android:id
=
"@+id/CustomDlgContentText"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:textSize
=
"15sp"
android:layout_margin
=
"5dp"
android:paddingLeft
=
"5sp"
/>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
android:layout_margin
=
"5dp"
>
<
Button
android:id
=
"@+id/CustomDlgButtonOK"
android:layout_width
=
"0dp"
android:layout_weight
=
"0.5"
android:layout_height
=
"wrap_content"
android:visibility
=
"gone"
/>
<
Button
android:id
=
"@+id/CustomDlgButtonCancel"
android:layout_width
=
"0dp"
android:layout_weight
=
"0.5"
android:layout_height
=
"wrap_content"
android:visibility
=
"gone"
/>
</
LinearLayout
>
</
LinearLayout
>
1
2
3
4
5
6
7
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
shape
android:shape
=
"rectangle"
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
solid
android:color
=
"#e6ecee"
/>
<
stroke
android:width
=
"1.0dip"
android:color
=
"@android:color/darker_gray"
/>
<
corners
android:radius
=
"8.0dip"
/>
</
shape
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package
com.ticktick.popdailog;
import
android.content.Context;
import
android.view.Gravity;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup.LayoutParams;
import
android.widget.Button;
import
android.widget.LinearLayout;
import
android.widget.PopupWindow;
import
android.widget.TextView;
public
class
CustomDailog {
private
View mParent;
private
PopupWindow mPopupWindow;
private
LinearLayout mRootLayout;
private
LayoutParams mLayoutParams;
//PopupWindow必须有一个ParentView,所以必须添加这个参数
public
CustomDailog(Context context, View parent) {
mParent = parent;
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
mRootLayout = (LinearLayout)mInflater.inflate(R.layout.custom_dailog,
null
);
mLayoutParams =
new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
}
//设置Dailog的标题
public
void
setTitle(String title) {
TextView mTitle = (TextView)mRootLayout.findViewById(R.id.CustomDlgTitle);
mTitle.setText(title);
}
//设置Dailog的主体内容
public
void
setMessage(String message) {
TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText);
mMessage.setText(message);
}
//设置Dailog的“确定”按钮
public
void
setPositiveButton(String text,OnClickListener listener ) {
final
Button buttonOK = (Button)mRootLayout.findViewById(R.id.CustomDlgButtonOK);
buttonOK.setText(text);
buttonOK.setOnClickListener(listener);
buttonOK.setVisibility(View.VISIBLE);
}
//设置Dailog的“取消”按钮
public
void
setNegativeButton(String text,OnClickListener listener ) {
final
Button buttonCancel = (Button)mRootLayout.findViewById(R.id.CustomDlgButtonCancel);
buttonCancel.setText(text);
buttonCancel.setOnClickListener(listener);
buttonCancel.setVisibility(View.VISIBLE);
}
//替换Dailog的“主体”布局
public
void
setContentLayout(View layout) {
TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText);
mMessage.setVisibility(View.GONE);
LinearLayout contentLayout = (LinearLayout)mRootLayout.findViewById(R.id.CustomDlgContentView);
contentLayout.addView(layout);
}
//设置Dailog的长宽
public
void
setLayoutParams(
int
width,
int
height) {
mLayoutParams.width = width;
mLayoutParams.height = height;
}
//显示Dailog
public
void
show() {
if
(mPopupWindow ==
null
) {
mPopupWindow =
new
PopupWindow(mRootLayout, mLayoutParams.width,mLayoutParams.height);
mPopupWindow.setFocusable(
true
);
}
mPopupWindow.showAtLocation(mParent, Gravity.CENTER, Gravity.CENTER, Gravity.CENTER);
}
//取消Dailog的显示
public
void
dismiss() {
if
(mPopupWindow ==
null
) {
return
;
}
mPopupWindow.dismiss();
}
}
1
findViewById(android.R.id.content)).getChildAt(
0
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
final
CustomDailog dailog =
new
CustomDailog(
this
,getRootLayout());
dailog.setTitle(
"Warning"
);
dailog.setMessage(
"This is ticktick's blog!"
);
dailog.setPositiveButton(
"OK"
,
new
OnClickListener() {
@Override
public
void
onClick(View v) {
dailog.dismiss();
}
});
dailog.setNegativeButton(
"Cancel"
,
new
OnClickListener() {
@Override
public
void
onClick(View v) {
dailog.dismiss();
}
});
dailog.show();
0 0
- 用PopupWindow实现自定义Dailog
- 自定义dailog注意事项
- 自定义PopupWindow的实现
- 自定义PopupWindow实现Spinner
- 分别用自定义PopupWindow和自定义Dialog实现下拉菜单
- PopupWindow,ListView实现自定义Spinner
- React-Native自定义PopupWindow实现
- React-Native自定义PopupWindow实现
- 自定义Dailog的样式中的一些属性
- 自定义Dailog,布局向上移动,windowFullscreen
- Android 自定义PopupWindow 实现 ListView与PopupWindow结合下拉框
- Android中PopupWindow自定义坐标实现
- Android中PopupWindow自定义坐标实现
- Android PopupWindow 实现自定义弹出层
- 使用PopupWindow实现全局自定义menu
- android自定义弹出框popupwindow方式实现
- 自定义PopupWindow实现3级地区联动
- Android中PopupWindow自定义坐标实现
- 玩转EditText控件
- Linux基础复习
- 九度 oj 题目1481:Is It A Tree?
- 判断两个浮点数是否相等
- Oracle连接关闭分析 Diagnosing the ORA-12537: TNS: connection closed error
- 用PopupWindow实现自定义Dailog
- 数据库事务管理
- 第一篇博客
- 深入了解EJB-EJB在java中的地位
- ABAP里OO ALV选中或container alv里的选中方法
- LeeCode Search in a Big Sorted Array java solution use binary search algorithm
- string.Format对C#字符串格式化
- android开发游记:RecycleView 实现复杂首页布局三种方式
- JVM,JRE,JDK联系