① android Dialog怎样自定义属性
你只要继承Dialog,然后加个xm样式
就可以了
样式:
<style
name="myDialogTheme"
parent="android:Theme.Dialog">
<!--
除去title
-->
<item
name="android:windowNoTitle">true</item>
</style>
重写Dialog
public
class
Mdi
extends
Dialog
{
public
Mdi(Context
context)
{
super(context);
}
public
Mdi(Context
context,int
style)
{
super(context,
style);
}
public
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mdi);
}
}
引用
Mdi
mdi
=
new
Mdi(FActivity.this,
R.style.myDialogTheme);
mdi.show();
② android加载自定义dialog,背景总是黑色的.不知道为什么.求解答
AlertDialog.Builder builder = new AlertDialog.Builder(UserInformationActivity.this,AlertDialog.THEME_HOLO_LIGHT);这种形式的,其中“AlertDialog.THEME_HOLO_LIGHT”设置背景,这个是白色的
把Activity设置成dialog的,修改AndroidManifest.xml中该Activity的theme,如:android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar" ,这样这个Activity会以dialog的形式弹出,并且是白色的
自己写一个自定义dialog继承dialog,你dialog的布局文件的最外层layout背景颜色设置成白色的
③ android怎么定义有选择的dialog
第一个需求:简单的自定义dialog
需求:创建一个dialog,该dialog具备以下功能:
1.有一个窗口可以显示文章
2.根据需求显示
1)点击同意(不同意),触发对应的事件(同意的事件会弹出一个Toast,不同意则会关闭程序)
2)点击关闭,关闭dialog
1.在main.xml文件中设定一个按钮,点击会弹出dialog
View Code
<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" >
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:id="@+id/btn"
android:background="@drawable/sl_btn_red" />
</RelativeLayout>2.创建dialog的内容布局,布局中设定了使用相对布局设定了三个按钮,其中两个一起出现(同意和不同意),另外一个单独出现(关闭)默认是两个出现
View Code
private TextView btnClose = null;
private TextView btnAgree = null;
private TextView btnDisagree = null;
private WebView mWebView = null;
public MyDialog(Context context) {
super(context, R.style.item_tnc_dialog);
setCancelable(false); // 阻止返回键的响应
setContentView(R.layout.dialog_view);
getWindow().setLayout(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
setUpView();
}
private void setUpView() {
mWebView = (WebView) findViewById(R.id.item_tnc_dialog_webview);
btnClose = (TextView) findViewById(R.id.item_tnc_dialog_close);
btnAgree = (TextView) findViewById(R.id.item_tnc_dialog_agree);
btnDisagree = (TextView) findViewById(R.id.item_tnc_dialog_disagree);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
btnDisagree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
System.exit(0);
}
});
btnAgree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "YOU SELECTED AGREE",
Toast.LENGTH_SHORT).show();
dismiss();
}
});
showDialog();
}
/** 加载webview的内容 */
public void showDialog() {
String localHtml = "file:///android_asset/los.html";
if (mWebView != null) {
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.loadUrl(localHtml);
}
buttonsDisplayTwo(false);
}
/** 当true的时候,出现同意和不同意两个选项,反之是关闭选项 */
private void buttonsDisplayTwo(boolean two) {
btnAgree.setVisibility(two ? View.VISIBLE : View.GONE);
btnDisagree.setVisibility(two ? View.VISIBLE : View.GONE);
btnClose.setVisibility(two ? View.GONE : View.VISIBLE);
}3.main.activity的代码就不写了。。直接写自定义的dialog代码
View Code
private TextView btnClose = null;
private TextView btnAgree = null;
private TextView btnDisagree = null;
private WebView mWebView = null;
public MyDialog(Context context) {
super(context, R.style.item_tnc_dialog);
setCancelable(false); // 阻止返回键的响应
setContentView(R.layout.dialog_view);
getWindow().setLayout(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
setUpView();
}
private void setUpView() {
mWebView = (WebView) findViewById(R.id.item_tnc_dialog_webview);
btnClose = (TextView) findViewById(R.id.item_tnc_dialog_close);
btnAgree = (TextView) findViewById(R.id.item_tnc_dialog_agree);
btnDisagree = (TextView) findViewById(R.id.item_tnc_dialog_disagree);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
btnDisagree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
System.exit(0);
}
});
btnAgree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "YOU SELECTED AGREE",
Toast.LENGTH_SHORT).show();
dismiss();
}
});
showDialog();
}
/** 加载webview的内容 */
public void showDialog() {
String localHtml = "file:///android_asset/los.html";
if (mWebView != null) {
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.loadUrl(localHtml);
}
buttonsDisplayTwo(false);
}
/** 当true的时候,出现同意和不同意两个选项,反之是关闭选项 */
private void buttonsDisplayTwo(boolean two) {
btnAgree.setVisibility(two ? View.VISIBLE : View.GONE);
btnDisagree.setVisibility(two ? View.VISIBLE : View.GONE);
btnClose.setVisibility(two ? View.GONE : View.VISIBLE);
}
④ android 自定义的dialog,edit text 不能获得焦点,弹出软键盘。
1、首先自定义布局
2、把自定义布局放入dialog中显示
3、通过自定义布局查找对应的edittext组件
final View DialogView = a .inflate ( R.layout.loand, null);//1、自定义布局
//创建对话框
AlertDialog dlg = new AlertDialog.Builder(loand.this)
.setTitle("登录框")
.setView(DialogView)//设置自定义对话框的样式,2、自定义布局放入dialog中显示
.setPositiveButton("登陆", //设置"确定"按钮
new DialogInterface.OnClickListener() //设置事件监听{
public void onClick(DialogInterface dialog, int whichButton){editText1 =(EditText) DialogView.findViewById(R.id.editText1);
editText2 =(EditText) DialogView.findViewById(R.id.editText2);//3、过自定义布局查找对应的edittext组件
String id = editText1.getText().toString();
String password = editText2.getText().toString();
⑤ [Android] 自定义 Dialog 布局设置固定宽高无效
Dialog 的自定义布局的根布局的宽度是写固定的,显示的时候宽度和高度不是对应的固定值。
根布局外面又添加了一层 FrameLayout,设置其宽高均为 wrap_content 来包裹以前的布局。
这个时候猜测是否因为添加自定义视图的时候,布局参数被改写了,然后开始查看源码,最终发现确实是这样的。
在下面的源码分析中,最终发现也是用了 mWindow.setContentView(mAlertDialogLayout) 将 R.layout.alert_dialog.xml 的默认布局添加到 PhoneWindow, 和Activity一样的。
关键的地方看一下 setupCustomContent() 这个方法,在添加自定义视图的时候布局参数设置为 MATCH_PARENT 了,所以我们设置固定大小是没有作用的,要套一层父布局解决这个问题。
⑥ android xutils 怎么定义dialog
做Android应用中,最缺少不了的就是自定义Dialog,对于系统默认提供的Dialog样式,一般都不复合应用的样式。
自定义Dialog需要3步骤即可:
1、主要的重写Dialog的java类
2、自定义布局文件、并设置Dialog Theme,在style.xml文件中加一个即可
3、使用方法
一、创建CustomPopDialog2.java类
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
/**
* 该自定义Dialog应用在:弹出框居中显示图片,点击其他区域自动关闭Dialog
*
* @author SHANHY([email protected])
* @date 2015年12月4日
*/
public class CustomPopDialog2 extends Dialog {
public CustomPopDialog2(Context context) {
super(context);
}
public CustomPopDialog2(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private Bitmap image;
public Builder(Context context) {
this.context = context;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public CustomPopDialog2 create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CustomPopDialog2 dialog = new CustomPopDialog2(context,R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_share_qrcode, null);
dialog.addContentView(layout, new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
dialog.setContentView(layout);
ImageView img = (ImageView)layout.findViewById(R.id.img_qrcode);
img.setImageBitmap(getImage());
return dialog;
}
}
}
这里简单说明下,我们自定义Dialog需要准备一个自己的View布局文件,主要关注create()方法即可,本例中就是直接显示一个图片。
二、自定义View的布局文件、并在style.xml中添加theme
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
android:id="@+id/rootLayout">
<ImageView
android:id="@+id/img_qrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="二维码" />
</LinearLayout>
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
三、使用自定义的Dialog
Bitmap bitmap = xxxxx;// 这里是获取图片Bitmap,也可以传入其他参数到Dialog中
CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context);
dialogBuild.setImage(bitmap);
CustomPopDialog2 dialog = dialogBuild.create();
dialog.setCanceledOnTouchOutside(true);// 点击外部区域关闭
dialog.show();
⑦ android中 自定义dialog用不用注册
dialog 不需要注册啊。
直接new出来,和系统的dialog一样的操作啊
new CustomDialog(context).show();
⑧ android怎样自定义dialog
在android学习中大家对于android很多东西,都有了新的了解或者说真正的掌握,关于dialog实例大家一定会经常用到,但如何才能更好的设计使它符合自己的需求,本篇将会为大家讲述。
第一个需求:简单的自定义dialog
需求:创建一个dialog,该dialog具备以下功能:
1.有一个窗口可以显示文章
2.根据需求显示
1)点击同意(不同意),触发对应的事件(同意的事件会弹出一个Toast,不同意则会关闭程序)
2)点击关闭,关闭dialog
1.在main.xml文件中设定一个按钮,点击会弹出dialog
View Code
<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" >
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:id="@+id/btn"
android:background="@drawable/sl_btn_red" />
</RelativeLayout>2.创建dialog的内容布局,布局中设定了使用相对布局设定了三个按钮,其中两个一起出现(同意和不同意),另外一个单独出现(关闭)默认是两个出现
View Code
private TextView btnClose = null;
private TextView btnAgree = null;
private TextView btnDisagree = null;
private WebView mWebView = null;
public MyDialog(Context context) {
super(context, R.style.item_tnc_dialog);
setCancelable(false); // 阻止返回键的响应
setContentView(R.layout.dialog_view);
getWindow().setLayout(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
setUpView();
}
private void setUpView() {
mWebView = (WebView) findViewById(R.id.item_tnc_dialog_webview);
btnClose = (TextView) findViewById(R.id.item_tnc_dialog_close);
btnAgree = (TextView) findViewById(R.id.item_tnc_dialog_agree);
btnDisagree = (TextView) findViewById(R.id.item_tnc_dialog_disagree);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
btnDisagree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
System.exit(0);
}
});
btnAgree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "YOU SELECTED AGREE",
Toast.LENGTH_SHORT).show();
dismiss();
}
});
showDialog();
}
/** 加载webview的内容 */
public void showDialog() {
String localHtml = "file:///android_asset/los.html";
if (mWebView != null) {
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.loadUrl(localHtml);
}
buttonsDisplayTwo(false);
}
/** 当true的时候,出现同意和不同意两个选项,反之是关闭选项 */
private void buttonsDisplayTwo(boolean two) {
btnAgree.setVisibility(two ? View.VISIBLE : View.GONE);
btnDisagree.setVisibility(two ? View.VISIBLE : View.GONE);
btnClose.setVisibility(two ? View.GONE : View.VISIBLE);
}3.main.activity的代码就不写了。。直接写自定义的dialog代码
View Code
private TextView btnClose = null;
private TextView btnAgree = null;
private TextView btnDisagree = null;
private WebView mWebView = null;
public MyDialog(Context context) {
super(context, R.style.item_tnc_dialog);
setCancelable(false); // 阻止返回键的响应
setContentView(R.layout.dialog_view);
getWindow().setLayout(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
setUpView();
}
private void setUpView() {
mWebView = (WebView) findViewById(R.id.item_tnc_dialog_webview);
btnClose = (TextView) findViewById(R.id.item_tnc_dialog_close);
btnAgree = (TextView) findViewById(R.id.item_tnc_dialog_agree);
btnDisagree = (TextView) findViewById(R.id.item_tnc_dialog_disagree);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
btnDisagree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
System.exit(0);
}
});
btnAgree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "YOU SELECTED AGREE",
Toast.LENGTH_SHORT).show();
dismiss();
}
});
showDialog();
}
/** 加载webview的内容 */
public void showDialog() {
String localHtml = "file:///android_asset/los.html";
if (mWebView != null) {
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.loadUrl(localHtml);
}
buttonsDisplayTwo(false);
}
/** 当true的时候,出现同意和不同意两个选项,反之是关闭选项 */
private void buttonsDisplayTwo(boolean two) {
btnAgree.setVisibility(two ? View.VISIBLE : View.GONE);
btnDisagree.setVisibility(two ? View.VISIBLE : View.GONE);
btnClose.setVisibility(two ? View.GONE : View.VISIBLE);
}
⑨ android datepickerdialog 怎么自定义
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/datePicker"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnersShown="true"
android:calendarViewShown="false"
/>
<TimePicker
android:id="@+id/timePicker"
android:layout_gravity="center_horizontal"
android:layout_width="145dp"
android:layout_height="wrap_content"/>
</LinearLayout>
自定义一个dialog,使用自定义布局,布局里面加入DatePicker(日期选择控件)或者TimePicker(时间选择控件),在主类中设置相应的监听器就能获取时间了。
当然这样制作出来的时间选择器的选择控件依然是系统自带的,只能有限的几种style,如果觉得不好看,只能自己使用各种基础控件和自定义逻辑来构建了。比如:用listView来滑动选择时间,用button来控制listView滚动等等。
纯手打~
⑩ Android如何调用其他的布局显示在对话框中
自定义Dialog。
1、编写自定义布局。
2、继承Dialog,覆盖构造方法。
3、覆盖onCreate(),初始化控件。以上就是Android调用其他的布局显示在对话框中的方法。