以编程方式设置 LinearLayout 中的边距
我正在尝试使用 Java(而不是 XML)创建一个 LinearLayout,其中的按钮填充屏幕并具有边距。这是没有边距的代码:
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r) {
Button btn = new Button(this);
btn.setText("A");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
这样工作得很好,但是你到底如何给按钮留出边距,以便它们之间有空间呢?我尝试使用 LinearLayout.MarginLayoutParams ,但它没有 Weight 成员,所以它不好。如果您在其构造函数中传递 lp
,它也不起作用。
这不可能吗?因为它确实看起来如此,而且它不会是您只能在 XML 中完成的第一个 Android 布局任务。
I'm trying to use Java (not XML) to create a LinearLayout with buttons that fill the screen, and have margins. Here is code that works without margins:
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r) {
Button btn = new Button(this);
btn.setText("A");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
So that works fine, but how on earth do you give the buttons margins so there is space between them? I tried using LinearLayout.MarginLayoutParams
, but that has no weight
member so it's no good. And it doesn't work if you pass it lp
in its constructor either.
Is this impossible? Because it sure looks it, and it wouldn't be the first Android layout task you can only do in XML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是完成它的一些代码:
Here is a little code to accomplish it:
您可以在
LinearLayout.LayoutParams
对象上调用setMargins()
。LinearLayout.LayoutParams
是LinearLayout.MarginLayoutParams
的子类,如 文档。不。
欢迎您提供此声明的证明。
就我个人而言,我不知道有什么事情只能通过 XML 来完成,而不能通过 SDK 中的 Java 方法来完成。事实上,根据定义,一切都必须通过 Java 来完成(尽管不一定通过 SDK 可访问的方法),因为 XML 不是可执行代码。但是,如果您知道某些事情,请指出来,因为这是 SDK 中的一个错误,有一天应该会得到修复。
You call
setMargins()
on theLinearLayout.LayoutParams
object.LinearLayout.LayoutParams
is a subclass ofLinearLayout.MarginLayoutParams
, as indicated in the documentation.No.
You are welcome to supply proof of this claim.
Personally, I am unaware of anything that can only be accomplished via XML and not through Java methods in the SDK. In fact, by definition, everything has to be doable via Java (though not necessarily via SDK-reachable methods), since XML is not executable code. But, if you're aware of something, point it out, because that's a bug in the SDK that should get fixed someday.
要直接向项目添加边距(某些项目允许直接编辑边距),您可以执行以下操作:
...这无需了解/编辑周围的布局即可工作。请注意“instanceof”检查,以防您尝试针对不支持边距的内容运行此检查。
To add margins directly to items (some items allow direct editing of margins), you can do:
...this works without needing to know about / edit the surrounding layout. Note the "instanceof" check in case you try and run this against something that doesn't support margins.
由于设备屏幕像素密度的变化,最好始终使用 DIP 单元以编程方式设置边距。就像下面_
希望这会有所帮助。
Due to variation in device screen pixel densities its good to always use
DIP
unit to set margin programmatically. Like below_Hope this will help.
我已经直接使用下面的代码设置了边距,
这里唯一需要注意的是,应该为以下包
android.widget.RelativeLayout.LayoutParams
导入LayoutParams
,否则将会出现一个错误。I have set up margins directly using below code
Only thing here is to notice that
LayoutParams
should be imported for following packageandroid.widget.RelativeLayout.LayoutParams
, or else there will be an error.试试这个
Try this
试试这个:
Try this:
你应该注意视图的viewGroup的类型。例如,在上面的代码中,我想更改frameLayout的边距,而frameLayout的视图组是一个RelativeLayout,所以你需要转换为(RelativeLayout.LayoutParams)
you should be ware of the type of the view's viewGroup.In the code above, for example,I want to change the frameLayout's margin,and the frameLayout's view group is a RelativeLayout,so you need to covert to (RelativeLayout.LayoutParams)
这是单行解决方案:
Here a single line Solution:
Core-KTX:
使用 ViewGroup 上的任何视图来代替 RecycleView。
Core-KTX:
Instead of RecycleView, use whatever your view is on ViewGroup.