0

我必须实现一个应该看起来像这样并且应该有一个特殊位置的自定义对话框,就像我需要将它附加到某个组件一样。

你能不能给我一些实现的想法?

我正在考虑覆盖现有的 Android Dialog 组件,但我不确定我是否可以像那样实现此功能。

高度赞赏任何链接参考或想法。 在此处输入图像描述

4

5 回答 5

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();
于 2013-09-05T12:25:40.630 回答
1

创建一个类的子Dialog类并在其中设置您的自定义视图setContentView,这将使您的代码更清晰。看这里的样本

如何在android中创建自定义对话框?

于 2013-09-05T12:40:32.633 回答
1

我有一个类似的要求,希望这会有所帮助。::

自定义对话类

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;
}

我的布局.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 我有我自己的列表,您可以根据需要更改内容,就像它可以是线性布局一样。希望这可以帮助

于 2013-09-05T12:54:22.173 回答
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();
                }
            });
于 2013-09-05T12:25:24.190 回答
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);

}

更多信息在这里

于 2013-09-05T12:29:32.130 回答