我必须实现一个看起来像这样的自定义对话框,并且应该有一个特殊的位置,比如我需要将它附加到某个组件。
你能不能给我一些实施想法?
我正在考虑覆盖现有的Android Dialog组件,但我不确定我是否可以像这样实现此功能。
非常感谢任何链接参考或想法。
答案 0 :(得分:1)
您需要做的就是创建一个与任何其他布局完全相同的XML并对其进行充气。
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//here is where you inflate your XML for the dialog
dialog.setContentView(R.layout.your_dialog_xml);
//now you can grab a reference to any component in your given xml like this
Button exampleButton = (Button) dialog.findViewById(R.id.my_xml_button);
//add any listeners etc.
//display your dialog
dialog.show();
答案 1 :(得分:1)
创建一个类Dialog
类的子类,并在其中设置setContentView
的自定义视图,这将使您的代码更清晰。看到这里的样本
答案 2 :(得分:1)
我有一个相同的要求,希望这会有所帮助。 ::
CUSTOM DIALOG CLASS
public class CustomDialogShape extends View {
public int startPointY,startPointX;
public int windowWidth;
public int dialogWidth, dialogHeight;
public int leftTop, rightTop;
public CustomDialogShape(Context context) {
super(context);
}
/**
*
* @param XPos
* @param YPos
* @param windowWidth
* @param dialogHeight
* @param dialogWidth
* Get the Click position, dialog dimension and window width from the parent window
* calculate the co-ordinates to draw the custom dialog shape
*/
public void setDimension(int XPos, int YPos, int windowWidth, int dialogHeight, int dialogWidth) {
this.startPointY = YPos;
this.startPointX = XPos;
this.windowWidth = windowWidth;
this.dialogHeight = dialogHeight;
this.dialogWidth = dialogWidth;
if(startPointX <= (windowWidth/2)) {
//Start Position is on the left half of the Screen
if(startPointX < (dialogWidth/2)) {
//Start position is on the leftmost end.
leftTop = 10;
rightTop = leftTop + dialogWidth;
} else {
leftTop = startPointX - (dialogWidth/2);
rightTop = leftTop + dialogWidth;
}
} else {
int rightSideRemaining = windowWidth - startPointX;
if(rightSideRemaining < (dialogWidth/2)) {
//Start position is on the leftmost end.
rightTop = windowWidth - 10;
leftTop = rightTop - dialogWidth;
} else {
rightTop = startPointX + (dialogWidth/2);
leftTop = rightTop - dialogWidth;
}
}
}
public CustomDialogShape(Context context, AttributeSet at) {
super(context, at);
}
/**
* Fill and Stroke Color
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// custom drawing code here
// remember: y increases from top to bottom
// x increases from left to right
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawPath(drawCustomShape(startPointX, startPointY, leftTop, rightTop), paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.GRAY);
canvas.drawPath(drawCustomShape(startPointX, startPointY, leftTop, rightTop), paint);
}
private Path drawCustomShape(int startPointX, int startPointY, int leftTop, int rightTop) {
Path pathFill = new Path();
pathFill.moveTo(startPointX, startPointY);
pathFill.lineTo(startPointX - 10, startPointY + 10);
pathFill.lineTo(leftTop, startPointY + 10);
pathFill.lineTo(leftTop, startPointY + 10 + dialogHeight);
pathFill.lineTo(rightTop, startPointY + 10 + dialogHeight);
pathFill.lineTo(rightTop, startPointY + 10);
pathFill.lineTo(startPointX + 10, startPointY + 10);
pathFill.lineTo(startPointX, startPointY);
pathFill.close();
return pathFill;
}
<强> MyLAYOUT.XML 强>
<?xml version="1.0" encoding="utf-8"?>
<com.cablevision.optimum2.widget.CustomDialogShape
android:id="@+id/custom_shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@android:color/transparent" />
<LinearLayout
android:id="@+id/list_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="100dip"
android:orientation="vertical" >
<ListView
android:id="@+id/stb_listVals"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="40dip"
android:clickable="false"
android:divider="#AA000000"
android:dividerHeight="7.3dip"
android:focusable="false"
android:scrollbarThumbVertical="@drawable/login_help_thumb"
android:scrollbarTrackVertical="@drawable/login_help_track"
android:scrollbars="vertical" >
</ListView>
</LinearLayout>
</FrameLayout>
在我的代码中
Rect r = locateView(activity.findViewById(View_where_you_touch));
float touchX= //get the touchx position by calculating through r.leftand r.right);
float touchY=r.bottom;
final Dialog dialog = new Dialog(Ctxt,
android.R.style.Theme_Translucent_NoTitleBar);
dialog.getWindow().
setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.MyLAYOUT);
CustomDialogShape custom = (CustomDialogShape) chnSTBDialog
.findViewById(R.id.custom_shape);
custom.setDimension( touchX, touchY, custom_shape_width,
custom_shape_height , totalwindowWidth));
public static Rect locateView(View view) {
Rect loc = new Rect();
int[] location = new int[2];
if (view == null) {
Logging.e(TAG, "locateView", "View not found");
}
在myLAYOUT XML中我有自己的列表,您可以根据需要更改内容,就像它可以是线性布局一样。希望这有帮助
答案 3 :(得分:0)
您可以构建如下自定义对话框:
Dialog mDialog;
mDialog = new Dialog(YourActivityName.this);
mDialog.setContentView(R.layout.checklist_navigatepop);//XML layout file
mDialog.setTitle("Navigation alert");
mDialog.setCancelable(true);
mDialog.show();
Button bt = (Button) mDialog.findViewById(R.id.button3);
Button bt1 = (Button) mDialog.findViewById(R.id.button2);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onBackPressed();
mDialog.dismiss();
}
});
答案 4 :(得分:0)
您不需要扩展Dialog类。
你需要这样的东西。
public Dialog buildDialog() {
Dialog messageDialog = new Dialog(context, android.R.style.Theme_Translucent);
messageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set your custom layout here.
messageDialog.setContentView(R.layout.dialog_wifi_disabled);
// Don't let the user cancel the dialog.
messageDialog.setCancelable(false);
messageDialog.setCanceledOnTouchOutside(false);
// You can get the views, like this.
TextView textView = (TextView) messageDialog.findViewById(R.id.your_view);
}
更多信息here