Change linear layout top margin programmatically android

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

i have two linear layouts in one frame layout.



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

    LayaoutParams usually create confusion while setting margin because of their parent layout... So this MarginLayoutParams is very useful which works with all layouts.

    Java Code

    MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
    params.width = 200; //Ths value 200 is in px... Please convert in DP
    params.leftMargin = 100; 
    params.topMargin = 200;
    

    Kotlin code

    val params: MarginLayoutParams = view!!.layoutParams as MarginLayoutParams
    params.width = 200 
    params.leftMargin = 100 
    params.topMargin = 200
    
    0 讨论(0)
提交回复
热议问题