Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式更改ImageView上的页边距-Android_Android_Imageview_Android Alertdialog_Margins - Fatal编程技术网

以编程方式更改ImageView上的页边距-Android

以编程方式更改ImageView上的页边距-Android,android,imageview,android-alertdialog,margins,Android,Imageview,Android Alertdialog,Margins,我正在用两个像按钮一样的ImageView创建AlertDialog,但我不知道如何设置第一个ImageView的右边距,因为我想在它们之间留有空间。我的代码: Context context = Main.this; AlertDialog.Builder alert = new AlertDialog.Builder(context); // Layoutot LinearLayout layout = new Linear

我正在用两个像按钮一样的ImageView创建AlertDialog,但我不知道如何设置第一个ImageView的右边距,因为我想在它们之间留有空间。我的代码:

        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();
我尝试过类似的方法,但我只显示了第一个(编辑)按钮


您可以为此使用
rightMargin

LinearLayout.LayoutParams margins = new LinearLayout.LayoutParams(Edit.getLayoutParams());
margins.rightMargin = 15;
Edit.setLayoutParams(margins);
LinearLayout.LayoutParams margins = new LinearLayout.LayoutParams(Edit.getLayoutParams());
margins.rightMargin = 15;
Edit.setLayoutParams(margins);