Android 安卓:对话框最小边距

Android 安卓:对话框最小边距,android,android-layout,android-dialog,Android,Android Layout,Android Dialog,我使用androidandroid.app.Dialog创建自定义对话框(用于按钮和背景) 在我的对话框中,我有一个文本视图在滚动视图中,而我有一个短文本,这正好显示了我想要的内容,但是如果文本非常大,我的对话框将全屏显示,我希望在对话框和屏幕边缘之间有一个最小的边距 我的问题是,我希望对话框不会比需要的大,并且我无法设置此对话框的固定大小 这就是今天的样子……我知道这是一个多么大的空白 GameDialog.java public class GameDialog extends Dial

我使用android
android.app.Dialog
创建自定义对话框(用于按钮和背景) 在我的对话框中,我有一个
文本视图
滚动视图
中,而我有一个短文本,这正好显示了我想要的内容,但是如果文本非常大,我的对话框将全屏显示,我希望在对话框和屏幕边缘之间有一个最小的边距

我的问题是,我希望对话框不会比需要的大,并且我无法设置此对话框的固定大小

这就是今天的样子……我知道这是一个多么大的空白


GameDialog.java

public class GameDialog extends Dialog {

    public GameDialog(Context ct, int titleID, int messageID) {
        super(ct, R.style.dialog_style);
        this.setContentView(R.layout.dialog_layout);

    }
    // This exist more code but this has noting with the layout to do,only set the text and show the button that exist since the XML file.

}
R.style.dialog\u style

<style name="dialog_style" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">?button_image</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textColor">#FF000000</item>
    <item name="android:textSize">20sp</item>
</style>

只需将布局边距添加到顶级视图组(线性布局)

此外,您有许多不必要的嵌套布局,不应该使用像素单位。您的对话框_layout.xml应该更像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="14dp" >

    <TextView
        android:id="@+id/text_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="14dp" 
        android:paddingLeft="14dp" />

    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="14dp" 
        android:paddingLeft="14dp">

        <TextView 
            android:id="@+id/text_main"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

    <LinearLayout
        android:id="@+id/layout_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
            <!-- this will be show while need to show a button -->
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
    </LinearLayout>
</LinearLayout> 

框架实际上是在背景图像本身上执行此操作的。换句话说,如果您查看为框架定义的主题,则其
windowBackground
属性只是一种清晰的颜色,即:

<item name="android:windowBackground">@android:color/transparent</item>

在这种情况下,填充不会转移到内容中,因此您还需要将填充添加到根布局项中,以使内容边界与图像显示相匹配。

抱歉,我的第一个答案误解了您的问题。没问题,您有其他IDE吗?这很好,我知道要查找基于android的代码,但有时很难找到一个包含所有数据并显示完整的*.java文件的工作页面。我们可以在运行时获取对话框的默认填充值吗?我就是找不到它的位置:(使用Hierarchy Viewer,我发现“对话框的默认填充”来自alertdialog本身及其父contentView。填充值都是硬编码的:
<item name="android:windowBackground">@android:color/transparent</item>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>
    <!-- This shape will be inset by the padding set above -->
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#262626" />
        </shape>
    </item>
</layer-list>