zoukankan      html  css  js  c++  java
  • Android Dialog自定义

    2011091614053019.png

    2011-9-17 16:56:03 上传
    下载附件 (42.45 KB)
     
     
     1 import android.app.Activity;
     2 import android.app.AlertDialog;
     3 import android.app.Dialog;
     4 import android.content.DialogInterface;
     5 import android.os.Bundle;
     6 import android.view.KeyEvent;
     7 import android.view.LayoutInflater;
     8 import android.view.Menu;
     9 import android.view.MenuItem;
    10 import android.view.View;
    11 import android.view.View.OnClickListener;
    12 import android.widget.Button;
    13 import android.widget.Toast;
    14 
    15 public class MyDialogActivity extends Activity {
    16     /** Called when the activity is first created. */
    17     @Override
    18     public void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.main);
    21         Button button = (Button) findViewById(R.id.button1);
    22         button.setOnClickListener(new OnClickListener() {
    23 
    24             @Override
    25             public void onClick(View v) {
    26                
    27                 //初始化一个自定义的Dialog
    28                 Dialog dialog = new MyDialog(MyDialogActivity.this,
    29                         R.style.MyDialog);
    30 
    31                 dialog.show();
    32             }
    33         });
    34 
    35     }
    36 }
    主布局文件main.xml
     
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical" android:layout_width="fill_parent"
     4     android:layout_height="fill_parent">
     5     <Button
     6         android:text="显示自定义Dialog"
     7         android:id="@+id/button1"
     8         android:layout_height="wrap_content"
     9         android:layout_width="fill_parent"/>
    10 </LinearLayout>

    新建一个自定义的Dialog类,取名MyDialog,继承自Dialog

     
     1 import android.app.Dialog;
     2 import android.content.Context;
     3 import android.os.Bundle;
     4 import android.view.LayoutInflater;
     5 import android.view.View;
     6 
     7 public class MyDialog extends Dialog {
     8 
     9     Context context;
    10     public MyDialog(Context context) {
    11         super(context);
    12         // TODO Auto-generated constructor stub
    13         this.context = context;
    14     }
    15     public MyDialog(Context context, int theme){
    16         super(context, theme);
    17         this.context = context;
    18     }
    19     @Override
    20     protected void onCreate(Bundle savedInstanceState) {
    21         // TODO Auto-generated method stub
    22         super.onCreate(savedInstanceState);
    23         this.setContentView(R.layout.dialog);
    24     }
    25 
    26 }

    相应的布局文件dialog.xml

     
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="wrap_content"
     5     android:layout_height="wrap_content"
     6     android:gravity="center_vertical|center_horizontal"
     7     android:background="@drawable/dialog_bg">
     8     <RelativeLayout
     9         android:layout_width="fill_parent"
    10         android:layout_height="wrap_content"
    11         android:paddingLeft="30dip"
    12         android:paddingTop="10dip">
    13         <ImageView
    14             android:id="@+id/dialog_title_image"
    15             android:layout_alignParentLeft="true"
    16             android:layout_width="wrap_content"
    17             android:layout_height="wrap_content"
    18             android:background="@drawable/dialog_title_image"/>
    19         <TextView
    20             android:layout_width="wrap_content"
    21             android:layout_height="wrap_content"
    22             android:layout_marginLeft="10dip"
    23             android:layout_centerInParent="true"
    24             android:text="Title"
    25             android:layout_toRightOf="@id/dialog_title_image"
    26             android:textColor="#000000"
    27             android:textSize="30sp"/>
    28         
    29     </RelativeLayout>
    30     <TextView
    31             android:layout_width="fill_parent"
    32             android:layout_height="1dip"
    33             android:background="@drawable/lins"
    34             android:layout_marginTop="5dip"/>
    35     <TextView
    36         android:layout_width="fill_parent"
    37         android:layout_height="wrap_content"
    38         android:text="This is a custom dialog"
    39         android:textColor="#000000"
    40         android:layout_marginTop="10dip"
    41         android:layout_marginLeft="30dip"/>
    42     <RelativeLayout
    43         android:layout_width="fill_parent"
    44         android:layout_height="wrap_content"
    45         android:paddingTop="10dip"
    46         android:gravity="bottom|center_horizontal"
    47         android:paddingBottom="10dip">
    48         <Button
    49             android:id="@+id/dialog_button_cancel"
    50             android:layout_alignParentLeft="true"
    51             android:layout_width="100dip"
    52             android:layout_height="wrap_content"
    53             android:text="确定"/>
    54         <Button
    55             android:id="@+id/dialog_button_ok"
    56             android:layout_width="100dip"
    57             android:layout_height="wrap_content"
    58             android:layout_toRightOf="@id/dialog_button_cancel"
    59             android:layout_marginLeft="35dip"
    60             android:text="取消"/>
    61     </RelativeLayout>
    62 </LinearLayout>

    最主要的,是自定义的Style,我们自定义一个式样,用来改变默认的Dialog样式

    在values文件夹下新建一个styles.xml文件,如下。。。

     
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <resources>
     3     <style name="MyDialog" parent="@android:Theme.Dialog">
     4         <item name="android:windowFrame">@null</item>
     5         <item name="android:windowNoTitle">true</item> 
     6         <item name="android:windowBackground">@drawable/dialog_bg</item>
     7         <item name="android:windowIsFloating">true</item>
     8         <item name="android:windowContentOverlay">@null</item>
     9     </style>
    10 </resources>

    这样应该就OK了

    <IGNORE_JS_OP>2011091614053019.png



    <IGNORE_JS_OP>2011091614174585.png



    另外一种方便的方法是:

     
     1 import android.app.Activity;
     2 import android.app.AlertDialog;
     3 import android.app.Dialog;
     4 import android.content.DialogInterface;
     5 import android.os.Bundle;
     6 import android.view.KeyEvent;
     7 import android.view.LayoutInflater;
     8 import android.view.Menu;
     9 import android.view.MenuItem;
    10 import android.view.View;
    11 import android.view.View.OnClickListener;
    12 import android.widget.Button;
    13 import android.widget.Toast;
    14 
    15 public class MyDialogActivity extends Activity {
    16     /** Called when the activity is first created. */
    17     @Override
    18     public void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.main);
    21         Button button = (Button) findViewById(R.id.button1);
    22         button.setOnClickListener(new OnClickListener() {
    23 
    24             @Override
    25             public void onClick(View v) {
    26 
    27 //                Dialog dialog = new MyDialog(MyDialogActivity.this,
    28 //                        R.style.MyDialog);
    29 
    30                 //此处直接new一个Dialog对象出来,在实例化的时候传入主题
    31 
    32                 Dialog dialog = new Dialog(MyDialogActivity.this, R.style.MyDialog);
    33 
    34                 //设置它的ContentView
    35 
    36                 dialog.setContentView(R.layout.dialog);
    37 
    38                 dialog.show();
    39             }
    40         });
    41 
    42     }
    43 }
  • 相关阅读:
    【Codeforces Round #645 (Div. 2) F】 Tasty Cookie
    【Codeforces Round #645 (Div. 2) E】 Are You Fired?
    【Educational Codeforces Round 88 (Rated for Div. 2) C】 Mixing Water
    20191114-4 Beta发布用户使用报告
    20191114-3 Beta阶段贡献分配
    beta发布
    20191107-1 每周例行报告
    beta 2/2 阶段中间产物提交入口
    beta week 2/2 Scrum立会报告+燃尽图 07
    beta week 2/2 Scrum立会报告+燃尽图 06
  • 原文地址:https://www.cnblogs.com/qingblog/p/2616960.html
Copyright © 2011-2022 走看看