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).
Related
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.
Please look at following 2 layouts.
First, cardview:
http://pastebin.com/iYzUiDQ7
Second, LinearLayout only (layout is actually the same as the one inside my CardView):
http://pastebin.com/xijHZCPw
So, second layout contains WebView component + single LinearLayout under it that should represent the same layout as single CardView item.
It works, however... I'm having problems with scaling images. If I set very large image as image for my ImageView placed on CardView, it will be scaled nicely to fit on my CardView. CardView's height won't be changed, instead, image will be scaled proportionally to fit.
But when I'm trying to do the same with second layout I linked, my LinearLayout changes its height if I set large image. How should I change my second layout to get the same effect for my LinearLayout, placed at bottom of my screen?
I get the difference between the two layout.
you had set the layout_height with 100dp in CardView. so the height of LinearLayout in CardView will does not beyond that. But you set the layout_height with wrap_content in you second LinearLayout.
Try to set maxHeight or layout_height with digit to deal with you problem.
Hope this can help you.
How do I center the text horizontally and vertically for a TextView in Android?
How do I center the alignment of a TextView?
In Linear Layouts use
android:layout_gravity="center"
In Relative Layouts
android:layout_centerInParent="true"
Above Code will center the textView in layout
To center the text in TextView
android:textAlignment="center"
android:gravity="center"
add this property for textview in xml
textview<android:height="yourheight"
android:width="yourwidth"
android:gravity="center">
your width size should be less than the parent container's width size
It depends on how you have declared the height and width. If you used wrap_content, then the textview is just big enough in that direction to hold the text, so the gravity attribute will not apply. android:gravity affects the location of the text Within the space allocated for the textview. layout_xx attributes affect the location of the view within it's parent.
SO if you set the size of the textview, then use gravity to position the text within the textview area, if you used wrap_content, then use the layout_xx attributes to position the textview within it's parent
I'd like to use the center of a View to align it in another View, let's say, a TextView in a RelativeLayout.
I don't want to have it centered inside the RelativeLayout but rather offset from the bottom left corner.
At the moment I align the TextView ParentLeft and ParentBottom and use margins on left and bottom to move it away from those sides.
However, because my TextView is set to wrap_content in width, it "moves" around when the text inside and with it its width changes (since the borders of the TextView are used for the offsets).
I want the center of the TextView to stay on my offsetted position.
Is this possible in the xml, without having to write code that manages the offset or to use another layout?
That's the unwanted behaviour when the text changes: Screen1
The desirable behaviour: Screen2
Note that the center of the TextView stays the same.
There is a simple logic here. If you want to align a view with respect to the parent then for relative layout you can use align_parent wherein in linear use android:layout_gravity. Here in you case make textview as match_parent and set gravity to centre.
I have a relative layout. For simplicity, there is an ImageView, EditText, and Button:
The ImageView is a banner with a fixed height
Below that is and the EditText where android:layout_width="fill_parent"
Below that is a Button with a fixed width and height
The problem is I want the editText to fill the leftover height of the screen. put and image on the top, a button on the bottom, and in the middle and edit text that takes up the rest of the space.
What property would i have to work with to get something similar to this?
For this purpose, there is LinearLayout with layout_weight property. Use LinearLayout for holding these 3 elements. For Button and ImageView, set layout_height as wrap_content. For EditText set layout_height="0dp" and layout_weight="1".
Have you considered using Relative Layout?
You could use
android:layout_below
android:layout_alignParentTop
android:layout_alignParentBottom
android:layout_marginTop
android:layout_marginBottom
Try that and post some sample code if you get stuck.