Change linear layout top margin programmatically android

后端 未结 7 1085
难免孤独
难免孤独 2021-02-01 02:31

i have two linear layouts in one frame layout.



        
相关标签:
7条回答
  • 2021-02-01 03:03

    use this

        layout = (LinearLayout) findViewById(R.id.layuout);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    
        layoutParams.setMargins(30, 20, 30, 0);
    layout.setLayoutParams(layoutParams );
    
    0 讨论(0)
  • 2021-02-01 03:04

    This updates the top margin without the need to update the other margin values.

    LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear_layout);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
    layoutParams.topMargin = 200;
    layout.setLayoutParams(layoutParams);
    
    0 讨论(0)
  • 2021-02-01 03:06

    I have set up margins directly using below code (I tried using LinearLayout.LayoutParams but is was not working for me)

    LinearLayout layout = (LinearLayout)findViewById(R.id.yourrelative_layout);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    params.setMargins(3, 300, 3, 3); 
    layout.setLayoutParams(params);
    

    Only this here is to notice that LayoutParams should be imported for following package android.widget.RelativeLayout.LayoutParams unless you will hit error.

    0 讨论(0)
  • 2021-02-01 03:10

    Use this method to set margin in dp

    private void setMargins (View view, int left, int top, int right, int bottom) {
        if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
    
            final float scale = getBaseContext().getResources().getDisplayMetrics().density;
            // convert the DP into pixel
            int l =  (int)(left * scale + 0.5f);
            int r =  (int)(right * scale + 0.5f);
            int t =  (int)(top * scale + 0.5f);
            int b =  (int)(bottom * scale + 0.5f);
    
            p.setMargins(l, t, r, b);
            view.requestLayout();
        }
    }
    

    Call the method :

    setMargins(linearLayout,0,0,5,0);
    

    https://stackoverflow.com/a/50951911/8523391

    0 讨论(0)
  • 2021-02-01 03:19

    Kotlin solution:

    fun LinearLayout.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
        val params: LinearLayout.LayoutParams = this.layoutParams as LinearLayout.LayoutParams
    
        val left = left?.let { it } ?: params.leftMargin
        val top = top?.let { it } ?: params.topMargin
        val right = right?.let { it } ?: params.rightMargin
        val bottom = bottom?.let { it } ?: params.bottomMargin
    
        params.setMargins(left, top, right, bottom)
        this.requestLayout()
    }
    

    then you just need to:

    layoutbtnlinear_aboutme.setMargins(top = 10)
    
    0 讨论(0)
  • 2021-02-01 03:21
       layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);
       LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
       params.setMargins(0, 50, 0, 0); 
       layout.setLayoutParams(params);
    
    0 讨论(0)
提交回复
热议问题