Is it possible to specify multiple gravity values in spinner item custom_style.xml?
Asked Answered
A

2

70

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.

Appointed answered 8/3, 2011 at 14:52 Comment(2)
What are you trying to accomplish? You can specify a horizontal gravity and a vertical gravity that way but two horizontal settings does not make sense.Sorn
My mistake, sorry. It was actually "left|center_vertical" of course. Now it works, thanks, for pointing out.Appointed
B
186

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 ]|

  1. Top-Left: android:layout_gravity="top|left"

  2. Top-Center: android:layout_gravity="top|center_horizontal"

  3. Top-Right: android:layout_gravity="top|right"

  4. Center-Left: android:layout_gravity="center_vertical|left"

  5. Very Center: android:layout_gravity="center"

  6. Center-Right: android:layout_gravity="center_vertical|right"

  7. Bottom-Left: android:layout_gravity="bottom|left"

  8. Bottom-Center: android:layout_gravity="bottom|center_horizontal"

  9. Bottom-Right: android:layout_gravity="bottom|right"

Bostwick answered 8/3, 2011 at 18:49 Comment(2)
Thanks for the answer, the "map" helped greatly! Nevertheless, I should mention, that I was intended to use exactly android:gravity attribute. My TextView goes as item layout parameter for ArrayAdapter of my spinner; android:layout_gravity didn't do any changes. Was close to solution, except for this silly positioning mistake, you and adamp pointed me out. Thanks again!Appointed
what is the meaning of: ` android:layout_gravity="center_vertical|right|end" `is there an contrudiction among the values?Dihedron
J
25

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.

Jupiter answered 1/1, 2018 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.