1. [代码]DialogShow.java 跳至 [1] [2] [3] [4] [全屏预览]
01 | package com.asfman; |
02 | |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import android.view.View; |
06 | import android.widget.Button; |
07 | |
08 | public class DialogShow extends Activity { |
09 | |
10 | /** Called when the activity is first created. */ |
11 | @Override |
12 | public void onCreate(Bundle savedInstanceState) { |
13 | super.onCreate(savedInstanceState); |
14 | setContentView(R.layout.main); |
15 | Button btn = (Button) findViewById(R.id.button1); |
16 | btn.setOnClickListener(new Button.OnClickListener() { |
17 | |
18 | @Override |
19 | public void onClick(View v) { |
20 | // TODO Auto-generated method stub |
21 | new Tip(DialogShow.this).show(); |
22 | } |
23 | |
24 | }); |
25 | } |
26 | } |
2. [代码]Tip.java 跳至 [1] [2] [3] [4] [全屏预览]
01 | package com.asfman; |
02 | |
03 | import android.app.Dialog; |
04 | import android.content.Context; |
05 | import android.view.Gravity; |
06 | import android.view.View; |
07 | import android.view.ViewGroup; |
08 | import android.view.Window; |
09 | import android.view.WindowManager; |
10 | import android.widget.ImageView; |
11 | |
12 | public class Tip { |
13 | |
14 | private ImageView image; |
15 | private Dialog mDialog; |
16 | |
17 | public Tip(Context context) { |
18 | mDialog = new Dialog(context, R.style.dialog); |
19 | Window window = mDialog.getWindow(); |
20 | WindowManager.LayoutParams wl = window.getAttributes(); |
21 | wl.x = -30; |
22 | wl.y = 20; |
23 | window.setAttributes(wl); |
24 | window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
25 | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); |
26 | //window.setGravity(Gravity.CENTER); |
27 | window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, |
28 | ViewGroup.LayoutParams.WRAP_CONTENT); |
29 | mDialog.setContentView(R.layout.tip); |
30 | mDialog.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0); |
31 | image = (ImageView) mDialog.findViewById(R.id.image); |
32 | image.setOnClickListener(new ImageView.OnClickListener() { |
33 | @Override |
34 | public void onClick(View arg0) { |
35 | mDialog.dismiss(); |
36 | } |
37 | }); |
38 | } |
39 | |
40 | public void show() { |
41 | mDialog.show(); |
42 | } |
43 | |
44 | } |
3. [代码]dialog.xml 跳至 [1] [2] [3] [4] [全屏预览]
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <resources> |
03 | <style name="dialog" parent="@android:style/Theme.Dialog"> |
04 | <!-- <item name="android:windowFrame">@null</item> --> |
05 | <!-- <item name="android:windowIsFloating">true</item> --> |
06 | <!-- <item name="android:windowIsTranslucent">false</item> --> |
07 | <item name="android:windowNoTitle">true</item> |
08 | <item name="android:windowBackground">@null</item> |
09 | <!-- <item name="android:backgroundDimEnabled">false</item> --> |
10 | </style> |
11 | </resources> |
4. [代码]tip.xml 跳至 [1] [2] [3] [4] [全屏预览]
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 | android:layout_width="300dp" android:layout_height="190dp" |
04 | android:orientation="vertical" android:background="@drawable/blueinfowindow"> |
05 | <LinearLayout android:layout_width="fill_parent" |
06 | android:layout_height="wrap_content" android:orientation="horizontal" |
07 | android:id="@+id/upContent" android:layout_marginTop="30dp" |
08 | android:layout_marginLeft="30dp"> |
09 | <TextView android:id="@+id/description" android:layout_width="220dp" |
10 | android:layout_height="wrap_content" |
11 | android:text="1.this is the test text!\n |
12 | 1.this is the test text!\n1.this is the test text!\n1.this is the test text!\n" android:textColor="#000000" /> |
13 | <ImageView android:id="@+id/image" android:background="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"/> |
14 | </LinearLayout> |
15 | </LinearLayout> |
本文展示了一个Android应用中自定义对话框的实现方法。通过创建`DialogShow`类继承`Activity`,并在其中设置按钮点击事件触发对话框显示。对话框通过`Tip`类定制样式,使用了自定义布局和透明背景。
640

被折叠的 条评论
为什么被折叠?



