【问题标题】:How to set margin dynamically in Android?如何在Android中动态设置边距?
【发布时间】:2012-06-19 04:52:42
【问题描述】:

我目前正在做一个包含自定义警报对话框的 android 应用程序。它包含一个按钮,但我无法设置按钮的边距。代码如下。 setmargin 方法不起作用

AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);

button.setText("Send");
LayoutParams buttonLayoutParams 
    = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

button.setLayoutParams(buttonLayoutParams);

resetPassword=editText.getText().toString();

LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);

myDialog.setView(layout);

【问题讨论】:

    标签: android margins


    【解决方案1】:

    这对我有用(支持库(需要使用 AndroidX):

    科特林

    val params = LinearLayoutCompat.LayoutParams(
                   LinearLayout.LayoutParams.WRAP_CONTENT,
                   LinearLayout.LayoutParams.WRAP_CONTENT
                   ).apply {
                     setMargins(0,16,0,16)
                   }
    

    Java

    LinearLayoutCompat.LayoutParams params = LinearLayoutCompat.LayoutParams(
                       LinearLayout.LayoutParams.WRAP_CONTENT,
                       LinearLayout.LayoutParams.WRAP_CONTENT
                       )
                       params.setMargins(0,16,0,16)
    

    【讨论】:

      【解决方案2】:

      写下面的代码来设置边距,它可能对你有帮助。

      AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
      Button button = new Button(Login.this);
      EditText editText = new EditText(Login.this);
      TextView textView = new TextView(Login.this);
      button.setText("Send");
      LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      buttonLayoutParams.setMargins(50, 10, 0, 0);
      button.setLayoutParams(buttonLayoutParams);
      String resetPassword = editText.getText().toString();
      LinearLayout layout = new LinearLayout(Login.this);
      layout.setOrientation(LinearLayout.VERTICAL);
      layout.addView(textView);
      layout.addView(editText);
      layout.addView(button);
      myDialog.setView(layout);
      myDialog.show();
      

      根据子视图的父布局使用LinearLayout.LayoutParamsRelativeLayout.LayoutParams

      【讨论】:

      • 不,它不工作。它显示了一个错误 add cast to buttonlayoutparams in setMargin 方法。
      • 请导入 import android.widget.LinearLayout.LayoutParams;在你的活动中。
      【解决方案3】:

      只是分享一个稍微不同的方法。

      您可以直接转换为 MarginLayoutParams,而不是转换为 LinearLayout.LayoutParamsRelativeLayout.LayoutParams 等。

      转换为 MarginLayoutParams 更好,因为您可以稍后更新您的布局,并且您无需返回到您的 java 代码以从 LinearLayout 更改为 RelativeLayout 或任何其他布局类型

      你可以这样做:

      MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
      
      // Set bottom margin
      layoutParams.bottomMargin = x;
      
      // Set top margin
      layoutParams.topMargin = x;
      
      // Set left margin
      // This won't have effect if you set any relative margin (start) previously or in the layout.xml
      layoutParams.leftMargin = x;
      
      // Set left margin
      // This won't have effect if you set any relative margin (end) previously or in the layout.xml
      layoutParams.rightMargin = x;
      
      // Set start margin
      layoutParams.setMarginStart(x);
      
      // Set end margin
      layoutParams.setMarginStart(x);
      
      // Set all left, top, right, bottom margins at once
      // Note that here, left and right margins are set (not start/end).
      // So, if you have used start/end margin before (or via layout.xml), 
      // setting left/right here won't have any effect.
      layoutParams.setMargins(left, top, end, bottom)
      
      // Then re-apply the layout params again to force the view to be re-draw
      // This step may not be necessary because depending where you set the margin, 
      // view is already scheduled to be drawn
      // For any case, to ensure the view will apply the new margin, call:
      view.setLayoutParams(layoutParams);
      

      【讨论】:

        【解决方案4】:
        You can set in LinearLayout margin
        
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams layoutParams = new
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(30, 20, 30, 0);
        Button okButton=new Button(this);
        okButton.setText("some text");
        ll.addView(okButton, layoutParams);
        

        【讨论】:

          【解决方案5】:

          setMargin() 方法在您使用 LinearLayout.LayoutParams 时可用,但在您使用 ViewGroup.LayoutParams 时不可用。 Dipak Keshariya 暗示了这一点,但并没有说太多。

          【讨论】:

            【解决方案6】:
            buttonLayoutParams.bottomMargin
            buttonLayoutParams.topMargin
            buttonLayoutParams.leftMargin
            buttonLayoutParams.rightMargin
            

            可用于设置边距

            【讨论】:

            • 你在为你的LinearLayout设置布局参数吗?
            猜你喜欢
            • 2012-10-01
            • 2015-07-06
            • 2021-08-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-01
            • 2016-02-28
            相关资源
            最近更新 更多