自定义Dialog
来源:互联网 发布:动易cms模板免费下载 编辑:程序博客网 时间:2024/05/17 02:24
1、显示的位置:
`
Window dialogWindow = this.getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值 dialogWindow.setGravity(Gravity.TOP|Gravity.LEFT);//显示位置
2、显示大小:
//屏幕宽度WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE);int width = wm.getDefaultDisplay().getWidth();int height = wm.getDefaultDisplay().getHeight();//显示大小lp.height = (int) (height * 1); // 高度设置为屏幕的0.6lp.width = (int) (width * 0.3); // 宽度设置为屏幕的0.65lp.alpha = 0.8f;dialogWindow.setAttributes(lp);
3、设置点击屏幕不消失:
this.setCanceledOnTouchOutside(false);//this是Dialog对象
4、SelectDialog代码如下:
import android.app.AlertDialog;import android.content.Context;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.Toast;import android.view.View.OnClickListener;public class SelectDialog extends AlertDialog implements OnClickListener{ Button text_color; Button background_color; Button add_friend; Button about_me; public SelectDialog(Context context) { super(context); } public SelectDialog(Context context, int theme) { super(context,theme); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.mydialog); setDialogSize(); initViews(); } private void initViews(){ text_color = (Button) findViewById(R.id.text_color); background_color = (Button) findViewById(R.id.background_color); add_friend = (Button) findViewById(R.id.add_friend); about_me = (Button) findViewById(R.id.about_me); text_color.setOnClickListener(this); background_color.setOnClickListener(this); add_friend.setOnClickListener(this); about_me.setOnClickListener(this); } public void setDialogSize() { //显示位置 Window dialogWindow = this.getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); dialogWindow.setGravity(Gravity.TOP|Gravity.LEFT); //屏幕宽度 WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); //显示大小 lp.height = (int) (height * 1); // 高度设置为屏幕的0.6 lp.width = (int) (width * 0.3); // 宽度设置为屏幕的0.65 lp.alpha = 0.8f; dialogWindow.setAttributes(lp); this.setCanceledOnTouchOutside(false); } @Override public void onClick(View v) { switch(v.getId()) { case R.id.text_color: Toast.makeText(getContext(), "text_color", Toast.LENGTH_SHORT).show(); break; case R.id.background_color: Toast.makeText(getContext(), "background_color", Toast.LENGTH_SHORT).show(); break; case R.id.add_friend: Toast.makeText(getContext(), "add_friend", Toast.LENGTH_SHORT).show(); break; case R.id.about_me : Toast.makeText(getContext(), "about_me", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getContext(), "default", Toast.LENGTH_SHORT).show(); break; } } }
5、MainActivity代码如下:
import android.app.Activity;import android.content.Context;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;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Context context = this; Button bt = (Button) this.findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SelectDialog dialog = new SelectDialog(context); dialog.show(); } }); }}
`
6、activity_main.xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dialog" android:id="@+id/bt" /></RelativeLayout>
7、mydialog.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" android:background="@drawable/dialog_background" > <TextView android:layout_width="wrap_content" android:layout_height="100dp" /> <Button android:background="#009999ff" android:textColor="#ffff66ff" android:textSize="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/text_color" android:text="字体颜色" /> <TextView android:layout_width="wrap_content" android:layout_height="20dp" /> <Button android:background="#009999ff" android:textColor="#ffff66ff" android:textSize="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/background_color" android:text="背景颜色" /> <TextView android:layout_width="wrap_content" android:layout_height="20dp" /> <Button android:background="#009999ff" android:textColor="#ffff66ff" android:textSize="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/add_friend" android:text="添加好友" /> <TextView android:layout_width="wrap_content" android:layout_height="20dp" /> <Button android:background="#009999ff" android:textColor="#ffff66ff" android:textSize="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/about_me" android:text="我的资料" /></LinearLayout>
0 0
- 自定义Dialog---继承Dialog
- 自定义Dialog android Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义dialog
- 自定义 Dialog
- 自定义dialog
- 自定义 dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 自定义dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义Dialog
- Hadoop学习(3)----HDFS API
- html css javascript命名规范
- 2015 辽宁省赛G题 边权排序
- 超详细的vim+cscope 的使用规则 转载
- 高斯滤波器-这个我觉得讲的最好
- 自定义Dialog
- Struts标签的简介~~~首先我不是搬运工
- [设计模式学习笔记]Bridge桥接模式
- JAVA环境变量设置
- 在fragment里面获取及添加actionbar
- sjtu 4020 数列游戏 动态规划
- 字符串处理——Tire树__LA3942
- Hibernate的基本配置~~感觉要跪了
- Training:组合博弈入门