I have one linearLayout inside another Linearlayout that fills most of the screen. When I set the gravity of the child layout to center, it only centers vertically, but does not center horizontally.
Midlayout is the child layout inside of top layout.
LayoutParams params=new LayoutParams(height,height);
params.gravity=Gravity.CENTER;
LayoutParams params2=new LayoutParams(MainActivity.screenWidth,MainActivity.screenHeight-height);
params2.gravity=Gravity.CENTER;
((LinearLayout)findViewById(R.id.arrange_midlayout)).setLayoutParams(params);
((LinearLayout)findViewById(R.id.arrange_toplayout)).setLayoutParams(params2);
LinearLayout only respects gravity on the opposite axis to it's orientation, e.g. if it's orientation is horizontal then it will only set the vertical gravity. You should probably use FrameLayout instead for the outer container.
Related
I have set the parent linear layout of a round corner effect. but the bottom rectangle corner of the embedded linear layout can't be clipped.
I have set the android:clipChildren="true" to the parent linear layout. and it still not work.
I have a ListView whose single item have a RelativeLayout which contains many elements , now i have RelativeLayout set to wrap_content in height and a certain width. What i want to display is horizontally center the RelativeLayout. ListView spans horizontally and vertically to full as it has also wrap_content in height and width.
Also i don't want to include another layout.
Change RelativeLayout width to match_parent. and manage all inner views as yout require.
I have a Activity conainting a header with some buttons(See picture).
You can switch the framelayout by clicking on the buttons. I have the framelayout set on wrap_content in the height for making the whole thing scrollable in a scrollview.
But I want to show only a map in the framelayout if you push on button2. And the height of the map need to be FILL_PARENT INSTEAD of WRAP_CONTENT. So I need to change the heigth of the FRAMELAYOUT.
Any ideas how I can do that?
This is what I tried:
ViewGroup.LayoutParams layoutParams = frameLayout.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;//MATCH_PARENT because FILL_PARENT is deprectated
frameLayout.setLayoutParams(layoutParams);
If I try to center the current view vertically within its parent. Which one should I use?
I saw TextViews use gravity="center_vertical" and ImageViews uselayout_centerVertical="true".
I am not sure why?
In textview , gravity="center_vertical" means the content in the textview will be center and vertical . you can only see the alignment of text if textview is fill_parent if its wrap_content then there will be no place for content to have alignment.If you use layout_gravity here and width and height to wrap_content then it will place the textview in the center _vertical of the parent layout.
In imageview ,layout_centerVertical="true". means place iamge and in center and vertical in the parent layout of image (i.e the container of image).
I have a table layout where each row is built programmatically.
The structure is basically, for each row:
TextView, LinearLayout, LinearLayout, LinearLayout, Button.
Each of the LinearLayout has multiple ImageViews inside of them. I want to increase the spacing between the ImageView items as they render with their borders touching.
I tried the suggestions here - In Android, how to make space between LinearLayout children? - I create the parameters like so:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
And set it like this to an image view when adding the control:
linearLayout1.AddView(image1);
linearLayout1.AddView(image2, layoutParams);
linearLayout1.AddView(image3);
Note I am only setting it on the middle item as a left & right margin on this would be fine for what I am trying to achieve.
The problem is that even without setting any margins on the layout params i.e. just instantiating it and setting it as shown above, it adds about a 35px margin to the left causing a much larger margin than I wanted. Even calling SetMargins with 1px doesn't change the margin.
Where am I going wrong?
set width of linear layout as WrapContent like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);