【问题标题】:Margin ignored by LinearLayoutLinearLayout 忽略边距
【发布时间】:2013-11-01 13:33:48
【问题描述】:

我正在创建包含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>

【问题讨论】:

  • 您是否尝试过使用 Hierarchy 查看器?或尝试将视图转储到 uiautomator?使用这些工具,您可以检查您的布局。您可以发布您正在使用的完整代码吗?
  • 能否请您发布更多代码?喜欢完整的 XML?我认为您将边距设置为错误的项目
  • 我使用 HierachyViewer 检查布局。虽然在代码中设置边距反映在布局的属性中,但我找不到 XML 文件中设置的值。
  • 我在问题中添加了外部布局...
  • 我遇到了同样的问题,如果我在 xml 文件中添加带有边距的布局之外的父线性布局,它可以工作。 Eclipse 声称额外的线性布局没用,但它确实有效。

标签: android layout android-linearlayout margin


【解决方案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>

【讨论】:

  • 您以编程方式添加到外部布局中的是一个布局还是两个嵌套布局?
  • 我的意思是,我可以相信 android:layout_margin 对于作为 XML 根的布局被忽略,但我不希望 android:layout_margin 在内部布局中正常工作(请检查它是否在您的环境中执行),除非此内部布局的父级以编程方式添加到其父级。
【解决方案2】:

您是为 inner 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 属性只适用于根元素
  • 我正在为另一个 LinearLayout 包含的 LinearLayout 设置属性。
【解决方案3】:

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

希望能成功

【讨论】:

  • 这将在外部视图中包含的所有视图周围设置一个空间,但不在包含的视图之间。
猜你喜欢
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 2020-06-26
  • 2020-09-22
  • 2019-12-12
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多