线性布局忽略了边距

12

我正在动态创建包含一个 LinearLayout 的视图,并将它们添加到外部的LinearLayout中。我想在创建的视图周围设置一个边距,但是XML文件中的layout_margin被忽略了。如果我在代码中设置参数,它可以工作,但我想在布局XML中指定边距。

在XML布局中设置边距将被忽略:

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

    ...
</LinearLayout>

创建时设置边距会被尊重:

LinearLayout productView = (LinearLayout) getLayoutInflater().inflate(R.layout.product_preview, null);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(50, 50, 50, 50);  
productView.setLayoutParams(params);

这是外部布局。视图将添加到dealer_activity_product_list中。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/dealer_activity_dealer_image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:contentDescription="@string/dealer_activity_dealer_image_desc" />

    <TextView
        android:id="@+id/dealer_activity_dealer_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/dealer_activity_product_list1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" />

        <LinearLayout
            android:id="@+id/dealer_activity_product_list2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" />
    </LinearLayout>
</LinearLayout>

:空段落标签。

你尝试过使用层次结构查看器吗?或者尝试将视图转储到uiautomator中吗?使用这些工具,您可以检查布局。您能贴出您正在使用的完整代码吗? - Tobrun
你能否请发更多的代码?比如完整的XML?我认为你将边距设置到了错误的项目上。 - Vladyslav Matviienko
我使用HierarchyViewer来检查布局。虽然在代码中设置边距会反映在布局属性中,但我找不到在XML文件中设置的值。 - multiholle
我在问题中添加了外部布局... - multiholle
我有同样的问题,如果我在xml文件中添加一个带有margin的父线性布局,它就可以正常工作。 Eclipse声称额外的线性布局是无用的,但它确实有效。 - Tore Rudberg
3个回答

1
你是为内部的LinearLayout还是包含的LinearLayout设置属性? 至少,在LinearLayout中以下内容可以正常工作:
<TextView
    android:id="@+id/xxx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:layout_margin="10dp"
    />

我相信作者为外部布局设置了边距。 xmlns属性仅适用于根元素。 - Vladyslav Matviienko
我正在为一个被另一个LinearLayout包含的LinearLayout设置属性。 - multiholle

-1
有一个常见的“嵌套布局”模式。 也就是说,您创建一个辅助容器布局,并在容器布局中定位内部布局以实现所需的效果。
类似这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        ...
        android:layout_margin="20dp"
        >
    ...
    </LinearLayout>
</LinearLayout>

你是在外部布局中以编程方式添加一个单一布局还是两个嵌套布局? - 18446744073709551615
我的意思是,我可以相信android:layout_margin在XML的根布局中被忽略,但我不会期望android:layout_margin在内部布局中正常工作(请检查它是否在您的环境中工作),除非将此内部布局的父布局以编程方式添加到其父布局中。 - 18446744073709551615

-2

在外部视图中使用 padding 而不是 layout_margin

希望它能够正常工作。


1
这将在外部视图中包含的所有视图周围设置空间,但不会在包含的视图之间设置。 - multiholle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接