android中scrollview中按钮之间的间距

时间:2014-01-06 11:56:32

标签: android scrollview

在我的应用程序中,我在滚动视图中以线性布局动态添加图像按钮。但是图像按钮之间没有间距。任何人都可以帮助我在图像按钮之间留出空间。 这是我的代码。

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null);

scrollview=(ScrollView)v.findViewById(R.id.scrollview);
LinearLayout ll =( LinearLayout)v.findViewById(R.id.scrollinear);

ImageButton img_travel = new ImageButton(this);


img_travel.setBackgroundResource(R.drawable.travel);

ImageButton img_work=new ImageButton(this);
img_work.setBackgroundResource(R.drawable.work);

ll.addView(img_work);
ll.addView(img_travel);

setContentView(v);

2 个答案:

答案 0 :(得分:1)

例如:

 LinearLayout.LayoutParams layoutParams =    new LinearLayout.LayoutParams(
 LinearLayout.LayoutParams.FILL_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
 layoutParams.setMargins(20, 20, 20, 20);


  ll.addView(img_work, layoutParams);

答案 1 :(得分:-2)

试试这个......

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null);

scrollview=(ScrollView)v.findViewById(R.id.scrollview);
LinearLayout ll =( LinearLayout)v.findViewById(R.id.scrollinear);

ImageButton img_travel = new ImageButton(this);


img_travel.setBackgroundResource(R.drawable.travel);

ImageButton img_work=new ImageButton(this);
img_work.setBackgroundResource(R.drawable.work);

View dummyView= new View(this);
LinearLayout.LayoutParams viewParams = new    LinearLayout.LayoutParams(your width in dp, your height in dp);

dummyView.setLayoutParams(viewParams )

ll.addView(img_work);
ll.addView(dummyView);
ll.addView(img_travel);

setContentView(v);