android:使用LayoutParams设置按钮之间的边距
我想自动垂直生成按钮,按钮之间的底部边距为20px。我尝试使用LayoutParams对象设置边距,但没有成功android:使用LayoutParams设置按钮之间的边距,android,button,layoutparams,Android,Button,Layoutparams,我想自动垂直生成按钮,按钮之间的底部边距为20px。我尝试使用LayoutParams对象设置边距,但没有成功 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/regions_search" android:layout_width="fill_paren
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/regions_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="30dip"
android:orientation="vertical" >
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
...
for (Region region : regionsList) {
//create new button
Button button = new Button(mContext);
//set infos
int id = Integer.parseInt(Long.toString((Long) region.getId())); button.setId(id);
button.setText(region.getName() + "( " + region.getStores_nb() + " )");
//Layoutparams setting
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 20);
button.setLayoutParams(params);
myLinear.addView(button);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
...
对于(区域:区域列表){
//创建新按钮
按钮按钮=新按钮(mContext);
//设置信息
intid=Integer.parseInt(Long.toString((Long)region.getId());button.setId(id);
button.setText(region.getName()+”(“+region.getStores_nb()+”);
//Layoutparams设置
FrameLayout.LayoutParams params=新建FrameLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容);
参数设置边距(0,0,0,20);
按钮。setLayoutParams(参数);
myLinear.addView(按钮);
}
正如你们在图片上看到的,图片之间并没有空间。有人知道为什么吗?
非常感谢。
尝试使用
LinearLayout.LayoutParams
而不是FrameLayout.LayoutParams
,就像在xml中使用LinearLayout
一样
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
您可以尝试以下方法:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
layoutParams.bottomMargin += 20;
button.setLayoutParams(layoutParams);
为什么要使用FrameLayout?XML文件在LinearLayout中。谢谢!你们所有人都给了我相同的答案,但你们是第一个发布的!谢谢~请注意这个Cata比你们快,对不起。谢谢你们的帮助