【问题标题】:It inflate the view without the margin它使没有边距的视图膨胀
【发布时间】:2011-12-04 14:04:15
【问题描述】:

我有这个代码

View item = View.inflate(context, R.layout.item_layout, null);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT);
    layout.addView(item, params);

我的 item_layout:(注意部分 android:layout_marginTop="2dip")

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_width="fill_parent">

    <ImageView android:src="@drawable/pic_unknown" android:id="@+id/image1"
        android:layout_height="50dip" android:layout_width="50dip"
        android:padding="5dip"></ImageView>
</RelativeLayout>

然后在我的布局中,我看到了膨胀的项目列表,但它们之间没有边距。我尝试使用 margintop=10dip 仍然没有发生我的观点是我在布局中放置的值在计算中没有考虑到有或没有边距顶部布局是相同的。

如何在项目之间添加一些空白空间? 如何充气项目之间的空白空间? 是否可以膨胀诸如 gap 或 some space 之类的东西? 或者我必须使用解决方法,比如用 2dip 高度或其他东西膨胀一些空布局 谢谢

【问题讨论】:

    标签: android margin inflate android-inflate layout-inflater


    【解决方案1】:

    inflate 方法的最后一个参数是您添加膨胀视图的参数。在您的情况下,它是null。试试这个:

     View item = View.inflate(context, R.layout.item_layout, layout);
    

    【讨论】:

    • 好建议。它是 attachToRoot。
    • 我遇到的问题是我将View 膨胀以将其附加到ListView 作为标题;如果我在充气机中使用ListView,它会引发异常。在这种情况下我将如何获得利润?
    • 布局是什么?
    • @SalehMosleh 父视图。您希望将我们添加到的视图。
    【解决方案2】:

    如果您的边距适用于外部,请尝试填充 RelativeLayout。

    【讨论】:

      【解决方案3】:

      您可以为您膨胀的布局添加边距,如下所示:

      final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                                      ViewGroup.LayoutParams.WRAP_CONTENT);
               params.topMargin = 10;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-12
        • 1970-01-01
        • 2016-09-29
        • 1970-01-01
        • 2017-01-08
        • 2011-08-13
        相关资源
        最近更新 更多