Layout orientation in code

后端 未结 6 959
暗喜
暗喜 2020-12-25 09:38

I have this code in my application:

LinearLayout.LayoutParams params =
    new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);

and I

相关标签:
6条回答
  • 2020-12-25 09:43

    You can use like this one:

    LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout);
    myll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    myll.setOrientation(LinearLayout.VERTICAL);
    
    0 讨论(0)
  • 2020-12-25 09:43

    A working sample below (it's LayoutParams.WRAP_CONTENT, NOT LinearLayout.WRAP_CONTENT)

    myLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    myLayout.setLayoutParams(layoutParams);
    
    0 讨论(0)
  • 2020-12-25 10:01

    You need to instance LinearLayout. After that you can call setOrientation()

    LinearLayout myLayout = ...;
    myLayout.setLayoutParams(new LayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
    myLayout.setOrientation(LinearLayout.VERTICAL);
    

    That should do the job :)

    For more infos check the Android API.

    0 讨论(0)
  • 2020-12-25 10:03

    Simply use as follow :-

    LinearLayout mlayout = new LinearLayout(context);
    mlayout.setOrientation(2);
    

    2 means Vertical, 1 is used for horizontal.

    0 讨论(0)
  • 2020-12-25 10:06

    You can't change LinearLayout's orientation using its LayoutParams. It can be done only with a LinearLayout object.

    LinearLayout layout = /* ... */;
    layout.setOrientation(LinearLayout.VERTICAL);
    
    0 讨论(0)
  • 2020-12-25 10:06

    In case anyone else arrives here like me looking for the answer for Xamarin, the equivalent is:

    LinearLayout layout = /* ... */;
    layout.Orientation = Orientation.Vertical;
    layout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
    
    0 讨论(0)
提交回复
热议问题