它们的定义dialog
(1)你需要准备自己的自定义对话框样式,是一个布局文件
<?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:background="@drawable/dialog_bg" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" > <RelativeLayout android:layout_width="321dp" android:layout_height="wrap_content" android:layout_weight="7.45" android:paddingLeft="30dip" android:paddingTop="10dip" > <ImageView android:id="@+id/dialog_title_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:background="@drawable/dialog_title_image" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="10dip" android:layout_toRightOf="@id/dialog_title_image" android:text="Title" android:textColor="#000000" android:textSize="30sp" /> </RelativeLayout> <TextView android:layout_width="270dp" android:layout_height="1dip" android:layout_marginTop="5dip" android:background="@drawable/lins" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dip" android:layout_marginTop="10dip" android:text="This is a custom dialog" android:textColor="#000000" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3.64" android:gravity="bottom|center_horizontal" android:paddingBottom="10dip" android:paddingTop="10dip" > <Button android:id="@+id/dialog_button_cancel" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="16dp" android:background="@drawable/btn_input_completed_normal" android:text="取消" /> <Button android:id="@+id/dialog_button_ok" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/dialog_button_cancel" android:layout_alignBottom="@+id/dialog_button_cancel" android:layout_marginLeft="48dp" android:layout_toRightOf="@+id/dialog_button_cancel" android:text="确定" /> </RelativeLayout> </LinearLayout>
该布局文件图片
(2)接下来要写一个自己定义的dialog类。载入上面的布局文件
package com.example.zidingyidialog; import android.app.Dialog; import android.content.Context; import android.os.Bundle; public class mydialog extends Dialog { Context context; public mydialog(Context context) { super(context); // TODO Auto-generated constructor stub this.context = context; } public mydialog(Context context, int theme){ super(context, theme); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.dialog); } }
(3)接下来写怎样出发该自己定义对话框,以及怎样对对话框上的button进行监听,以便作出响应
package com.example.zidingyidialog; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnShowListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ private Dialog dialog ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //初始化一个自己定义的Dialog dialog = new mydialog(MainActivity.this, R.style.MyDialog); dialog.show(); Button button = (Button) dialog.findViewById(R.id.dialog_button_ok); button.setOnClickListener(l); Button butto = (Button) dialog.findViewById(R.id.dialog_button_cancel); butto.setOnClickListener(l1); ; } }); } OnClickListener l = new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }; OnClickListener l1 =new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "hhahha", Toast.LENGTH_SHORT).show(); } }; ; }
还有基本的就是在values下的styles中加入例如以下内容,便于控制对话框的格式
</style> <style name="MyDialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/dialog_bg</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
以上就是加入自己定义对话框的基本步骤
版权声明:本文博客原创文章,博客,未经同意,不得转载。