如何保证linearlayout_如何仅指定一次LinearLayout元素之间的间距?

您可以在单独的xml文件中定义单个项目“prototype”,然后在代码中动态扩充该文件中的项目并将它们插入到线性布局中。

然后,您可以在实际项目上定义间距,而不是父级LinearLayout(例如,作为android:layout_marginTop),并且在您向它们充气时,该间距将应用于您的所有项目。

编辑:

container.xml中:

android:id="@+id/parent"

android:layout_width="match_parent"

android:layout_height="match_parent">

item.xml:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="4dp">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="This is my child" />

MyActivity.java:

// Put this in a suitable place in your Java code, perhaps

// in "onCreate" or "onResume" depending on where and how

// you initialize your view. You can, of course inflate

// any number of instances of the item and add them to

// your parent LinearLayout.

LayoutInflater inflater = LayoutInflater.from(context);

View item = inflater.inflate(R.layout.item, null, false);

LinearLayout container = findViewById(R.id.parent);

container.addView(view);我没有努力测试代码,但它“应该”按原样工作:-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值