0
我使用兩個ImageViews lika按鈕創建AlertDialog,但我不知道如何設置第一個ImageView的右邊距,因爲我想在它們之間留出空間。我的代碼:以編程方式更改ImageView上的邊距 - Android
Context context = Main.this;
AlertDialog.Builder alert = new AlertDialog.Builder(context);
// Layoutot
LinearLayout layout = new LinearLayout(context);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, // width
LayoutParams.WRAP_CONTENT)); // height
// Image Edit
ImageView Edit = new ImageView(context);
Edit.setImageResource(R.drawable.edit);
Edit.setBackgroundResource(R.drawable.button);
Edit.setPadding(5, 20, 5, 20);
Edit.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, // width
LayoutParams.WRAP_CONTENT, // height
1f)); // weight
// Image Delete
ImageView Delete = new ImageView(context);
Delete.setImageResource(R.drawable.delete);
Delete.setBackgroundResource(R.drawable.button);
Delete.setPadding(5, 20, 5, 20);
Delete.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, // width
LayoutParams.WRAP_CONTENT, // height
1f)); // weight
layout.addView(Edit);
layout.addView(Delete);
alert.setView(layout);
alert.show();
我已經試過這樣的事情,但我也只顯示第(編輯)按鈕
LinearLayout.LayoutParams margins = new
LinearLayout.LayoutParams(Edit.getLayoutParams());
margins.setMargins(0, 0, 15, 0);
Edit.setLayoutParams(margins);
同樣的......第二張圖片不在屏幕上.. – KiKo