是否可以在微调项目 custom_style.xml 中指定多个重力值?
我有 textview.xml,它是微调器的项目样式。
<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>
我知道可以同时指定 (Gravity.LEFT | Gravity.CENTER_HORIZONTAL)
,但在 xml 中不起作用 - 文本仅移动到左侧。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
87element,
我相信你打算使用
layout_gravity
你只使用gravity
?是的,您可以将其中两个
layout_gravity
属性与“|”组合在一起如文档中所述: http://developer.android.com/参考/android/R.attr.html#layout_gravity但即使使用
layout_gravity
而不仅仅是gravity
(如 adamp 在他的评论中所述),您的设置试图结合是没有意义的。您基本上告诉它沿同一行左对齐和居中。在这种情况下,left
和center_horizontal
都指的是水平位置。您试图在哪里相对于父级对齐
TextView
?您的父布局就像一个网格,您可以在其中选择一个位置:
|[ 1 ] [ 2 ] [ 3 ]|
|[ 4 ] [ 5 ] [ 6 ] |
|[7][8][9]|
左上:
android:layout_gravity="top|left"
中上:
android:layout_gravity="top|center_horizontal"
右上:
android:layout_gravity="top|right"
中左:
android:layout_gravity="center_vertical|left"
非常居中:
android:layout_gravity="center"
居中偏右:
android:layout_gravity="center_vertical|right"
左下:
android:layout_gravity="bottom|left"
中下:
android:layout_gravity="bottom|center_horizontal"
右下:
android:layout_gravity="bottom|right"
87element,
I believe you intended to use
layout_gravity
you are only usinggravity
??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_gravityBut even using
layout_gravity
instead of justgravity
(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. Bothleft
andcenter_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"
以防万一有人和我一样愚蠢。不要忘记删除空格。
android:gravity="center_horizontal|bottom"
但
android:gravity="center_horizontal | Bottom"
无效。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.