简体   繁体   English

安卓 如何使对话框按钮在对话框外出现一些

[英]Android. How to make dialog buttons appear a bit outside the dialog

I need to have a dialog (it's a game dialog) where buttons are at the lower corners of the dialog. 我需要有一个对话框(这是游戏对话框),其中按钮位于对话框的下角。 Not inside the dialog but rather on the very corners (ie part of the button will reside over the dialog and the part will be outside of it). 不在对话框内部 ,而是在角落(即,按钮的一部分将位于对话框上方,而部分将位于对话框外部)。

First, as far as I know you can't move layout children outside their parent. 首先,据我所知,您不能将布局子级移到其父级之外。

I've never tried exactly what you're going for, but I think it can be done. 我从未尝试过您要做什么,但我认为可以做到。 The trick would be to go with an activity with a dialog theme (you can find examples of these on the developer site or the API demos). 技巧是进行带有对话框主题的活动(您可以在开发人员站点或API演示中找到这些示例)。 Make sure your layout's root node has width and height set to wrap_content. 确保布局的根节点的宽度和高度设置为wrap_content。 Your root layout should be a RelativeLayout and have NO background (android:background="#0000"). 您的根目录布局应为RelativeLayout且没有背景(android:background =“#0000”)。

Next, add another layout to your root node (FrameLayout would probably work) with a custom drawable for a background (or use the one that the default dialog uses from the framework) and width and height set to fill_parent or match_parent. 接下来,使用自定义可绘制背景为背景(或使用框架中默认对话框使用的自定义图形)并将宽度和高度设置为fill_parent或match_parent,向根节点添加另一种布局(FrameLayout可能会起作用)。 Set android:padding to some dip value which pulls the background in from the edge of the dialog. 将android:padding设置为某个dip值,该值将从对话框的边缘拉入背景。

The only thing left to do would be to add your other layout elements to the root node. 剩下要做的就是将其他布局元素添加到根节点。 The FrameLayout will be drawn beneath everything else, and the padding will create the illusion of borders which do not encompass your UI. FrameLayout将绘制在其他所有内容的下方,并且填充将创建不包含UI的边框错觉。

Update Yikes, just tried the above with good and bad results. 更新 Yikes,只是尝试了上述结果,但结果却不尽人意。 First, you'll definitely want to look at the "Custom Dialog" example from the API demo, which makes use of: 首先,您肯定要看一下API演示中的“自定义对话框”示例,该示例利用了:

Create an activity which uses the above xml layout file, and set the style for the activity to Theme.CustomDialog that you defined in xml/styles.xml. 创建一个使用上述xml布局文件的活动,并将该活动的样式设置为您在xml / styles.xml中定义的Theme.CustomDialog。 This will get you a red background for your activity. 这将为您的活动提供红色背景。 You can then edit the filled_box shape file to just have one background attribute set to invisible ("#0000"). 然后,您可以将fill_box形状文件编辑为仅将一个背景属性设置为不可见(“#0000”)。 The result should be an dialog-shaped activity with no background. 结果应该是没有背景的对话框形状的活动。

Next I tried to hack a background using my thoughts from above. 接下来,我尝试使用上面的想法来破解背景。 The idea should be that there's a phony background drawn behind the other UI elements which does not encompass them, so it could be "shrunk" using layout_margin and not affect them. 这个想法应该是在其他UI元素后面绘制一个假的背景,而不包含它们,因此可以使用layout_margin将其“缩小”并且不影响它们。 The problem here is that the phony background needs to have width and height set to relative to the other UI elements, so it sort of HAS to encompass them, so it can properly measure its own width and height relative to them. 这里的问题是,假音背景需要将其宽度和高度设置为相对于其他UI元素的高度,因此它需要通过HAS来包含它们,以便可以正确地测量其相对于它们的宽度和高度。

So I think the solution could be to do most of what I've said above, except don't try the phony background thing. 因此,我认为解决方案可能是做我上面所说的大部分事情,除非不要尝试虚假的背景操作。 Just use a 9-patch drawable for your root layout background, and shrink the edges of your background to be drawn farther in than your content. 只需对根布局背景使用9补丁可绘制对象,然后将背景边缘缩小到比内容更远的位置即可。 You'd still use the custom theme stuff from above with an invisible window theme. 您仍将上面的自定义主题内容与不可见的窗口主题一起使用。

Here is a sample layout which i tried: 这是我尝试的示例布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >
<FrameLayout
android:orientation="vertical"
android:id="@+id/ll1" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/dialog_frame">
</FrameLayout>
<Button android:layout_width="wrap_content"
 android:id="@+id/button1" 
 android:layout_height="wrap_content"
 android:layout_marginTop="35dp"
 android:layout_centerHorizontal="true"
  android:text="Button"></Button>
</RelativeLayout>

here is the screenshot: 这是屏幕截图: 布局样本

hope u get the hint , goodluck 希望你能得到提示,祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM