Android AlertDialog自定义布局失真

Android AlertDialog自定义布局失真,android,xml,android-alertdialog,Android,Xml,Android Alertdialog,您好,我想知道alertdialogs如何准确地膨胀自定义xml布局?在我的示例中,当我在eclipse编辑器中查看xml时,它看起来很好,但是当alertdialog构建它时,我看起来完全不同。(遗憾的是,我没有发布截图的声誉) 这里是xml。可以说,布局被推高了,所以线性布局的下半部分可能占据了页面的三分之二,尽管它们的重量只有0.40(ps,如果有人能告诉我这些线性布局的替代方案,我将不胜感激!!) 多谢各位 <?xml version="1.0" encoding="utf-8"?

您好,我想知道alertdialogs如何准确地膨胀自定义xml布局?在我的示例中,当我在eclipse编辑器中查看xml时,它看起来很好,但是当alertdialog构建它时,我看起来完全不同。(遗憾的是,我没有发布截图的声誉)

这里是xml。可以说,布局被推高了,所以线性布局的下半部分可能占据了页面的三分之二,尽管它们的重量只有0.40(ps,如果有人能告诉我这些线性布局的替代方案,我将不胜感激!!) 多谢各位

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
 >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.20"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.98"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:paddingLeft="20dp"
            android:paddingTop="32dp"
            android:text="Share with..."
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#00C5CD" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.02"
        android:orientation="vertical" >

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#00C5CD" >
        </View>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.40"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.50"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.50"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.350"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.350"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/iv_prev"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/facebook" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.150"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/tv_customize_pill"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:paddingTop="10dp"
                    android:text="Whatsapp"
                    android:textAppearance="?android:attr/textAppearanceSmall" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.150"
                android:orientation="vertical" >
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.50"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.350"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.350"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/iv_preview3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/whatsapp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.150"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/tv_customize_pillsas"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:paddingTop="10dp"
                    android:text="Whatsapp"
                    android:textAppearance="?android:attr/textAppearanceSmall" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.150"
                android:orientation="vertical" >
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.40"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="0.50"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="0.150"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="0.350"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/iv_preview4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/email" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="0.150"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_customize_pill4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingTop="10dp"
                android:text="Email"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="0.350"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.50"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>


好吧,我知道你需要的只是一个标题,下面是一个网格状的布局,对吗??如果我是对的,
TableLayout
可能就是您所需要的

检查这个xml,看看它是否对您有帮助

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:paddingLeft="20dp"
        android:paddingTop="32dp"
        android:text="Share with..."
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#00C5CD" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="#00C5CD"/>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="Some text"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="Some text"/>

            </LinearLayout>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="Some text"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="Some text"/>

            </LinearLayout>

        </TableRow>

    </TableLayout>
</LinearLayout>


你是认真对待这个布局文件的吗??这么多嵌套的线性布局,其中大多数甚至没有任何意义。我试图理解你们想要用这个实现什么?一个标题,然后在下面把屏幕分成4部分。每件作品都有一个图像视图和一个文本视图。所以基本上,在标题屏幕被垂直和水平地分成两部分后,在左上、右上、左下和右下各留下四个相同的线性布局。不,不管你的动机是什么,我想说的是这没有意义。为什么需要围绕一个文本视图和图像视图进行线性布局。它没有任何作用。linearlayout的目的是在一行或一列中对齐两个或多个子视图。我知道这是一种非常低效的方法,但让我试着说明我的观点。就说我有一个屏幕,我想要一个图像在它的中心。我想要图像与背景的比率保持不变,不管屏幕大小是什么。填充不会这样做,因为dp是相对的(对小屏幕的影响大于对大屏幕的影响),而如果将其拆分为线性布局,则将图像视图设置为所需的线性布局,而不是该图像视图的大小将始终采用父线性布局指定的大小,无论屏幕大小看起来如何正确谢谢,我仍然不知道alertdialog会怎么膨胀,但若一切顺利,我会接受这个答案。再次感谢!基本上清理了膨胀的视图!仍然有一些小问题,但它们应该是可管理的!谢谢你!很高兴我能帮忙