我想自動生成垂直按鈕之間的下邊距爲20px的按鈕。我嘗試使用LayoutParams對象設置邊距,但沒有成功。android:在按鈕之間設置頁邊距與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_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);
}
正如您在圖像上看到的那樣,圖像之間沒有空間。有人知道爲什麼嗎? 謝謝!
爲什麼您使用的FrameLayout? XML文件在LinearLayout中。 –
完成,謝謝 – johann