LinearLayout忽略了保证金

mul*_*lle 12 layout android margin android-linearlayout

我正在创建包含a的动态视图LinearLayout并将它们添加到外部LinearLayout.我想在创建的视图周围设置边距,但layout_margin忽略XML文件中的边距.如果我在代码中设置参数,它可以工作,但我想在布局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>
Run Code Online (Sandbox Code Playgroud)

在创建时设置边距是值得尊重的:

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);
Run Code Online (Sandbox Code Playgroud)

这是外部布局.视图已添加到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>
Run Code Online (Sandbox Code Playgroud)

KDe*_*kar -2

设置padding而不是layout_margin在外部视图中。

希望它能起作用