I have textview.xml, which is the item style for spinners.
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="left|center_horizontal" android:textSize="18sp"> </TextView>
I know that it is possible to specify both (Gravity.LEFT | Gravity.CENTER_HORIZONTAL)
, but in xml that doesn't work - text is moved to the left only.
Gravity and layout_gravity both are the XML attributes. The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget). The android:layout_gravity is used to arrange the position of the entire View relative to it's container.
The android:layout_gravity is an attribute that sets the gravity of the View or Layout in its parent. The android:layout_gravity is used to control the gravity of an individual view in a container. It is the outside gravity of the View.
87element,
I believe you intended to use layout_gravity
you are only using gravity
??
Yes, you can combined two of these layout_gravity
attributes together with the '|' as mentioned in the documentation: http://developer.android.com/reference/android/R.attr.html#layout_gravity
But even using layout_gravity
instead of just gravity
(as adamp stated in his comment), the settings you are trying to combine don't make sense. You are telling it basically to align left and center along the same row. Both left
and center_horizontal
in this case refer to horizontal positions.
Where are you trying to align the TextView
relative to the parent?
Your parent layout is like a grid where you can select a position:
|[ 1 ] [ 2 ] [ 3 ]|
|[ 4 ] [ 5 ] [ 6 ]|
|[ 7 ] [ 8 ] [ 9 ]|
Top-Left: android:layout_gravity="top|left"
Top-Center: android:layout_gravity="top|center_horizontal"
Top-Right: android:layout_gravity="top|right"
Center-Left: android:layout_gravity="center_vertical|left"
Very Center: android:layout_gravity="center"
Center-Right: android:layout_gravity="center_vertical|right"
Bottom-Left: android:layout_gravity="bottom|left"
Bottom-Center: android:layout_gravity="bottom|center_horizontal"
Bottom-Right: android:layout_gravity="bottom|right"
Hope this helps!
Just in case anyone else was as stupid as me. Don't forget to remove spaces. android:gravity="center_horizontal|bottom"
but android:gravity="center_horizontal | bottom"
isn't valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With