问题描述
我有我的应用程序的一些TextViews,我不知道为什么,但安卓重力
属性不居中的文本内容,它应该是上运行的设备API 18+(4.3 +)。
I have some TextViews on my app, I don't know why but the android:gravity
attribute is not centering the text content where it should be on devices running the API 18+ (4.3+).
还有就是code我在我的自定义使用的TextView
,这是RelativeLayout的一个孩子:
There is the code I use on my custom TextView
, this is a child of RelativeLayout:
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/seekbar"
android:layout_marginLeft="@dimen/margin_tiny_double"
android:layout_toLeftOf="@+id/seekbar"
android:gravity="center_vertical|left"
android:paddingRight="@dimen/margin_tiny"
android:text="@string/text1"
android:textColor="@color/black"
android:textSize="@dimen/size_text_normal" />
这code应该抓住边缘的TextView
键,将其调整到顶部和底部,并把它放到左侧的搜索栏
,这是工作,但的TextView
变大,所以用安卓重力
我中心文本中心,从它的自我离开了。它的工作原理,但我不知道为什么,文本不集中在中心|留在设备运行的是Android 4.3和4.4。这个问题可以再现真实的设备和以及在布局上preVIEW的Eclipse(图形布局)。
This code should take the edges of this TextView
and align it to the top and bottom and put it to the Left of the SeekBar
, this is working, but the TextView
gets big, so with android:gravity
I center the text to the center and left from it self. It works, but I don't know why, the text is not centered at center|left on devices running android 4.3 and 4.4. This issue can be reproduced on real devices and as well on the layout preview (Graphic layout) of Eclipse.
有或安卓作出API 18+的任何变化:重力
我失踪?
PS:我打靶的API 19的项目,并在AndroidManifest.xml中
PS: I'm targetting the API 19 on the project and on AndroidManifest.xml
PS2:我TextView的是自定义的只是设置一个外部font.tff
PS2: My TextView is custom just to set an external font.tff
这是怎么看起来像API 17日 -
这是怎么看起来像API 18 +
在此先感谢!
= UPTADATE =
在我的清单,我改变了安卓targetSdkVersion
17,而不是19,问题消失了,但这是一个绝招,而不是一个解决方案,又能怎样我这样做,因为它可能是从API的问题?是的,我有API的18和19(今天,二○一四年一月三十零日)的最新版本。
On my Manifest, I changed the android:targetSdkVersion
to 17 instead of 19 and the problem disappeared, but this is a "trick", not a solution, what can I do since it could be an issue from the API ? And yes, I have the latest version of the API 18 and 19 (today, 01/30/2014).
推荐答案
这似乎是API的一个已知问题18 +:
This appears to be a known issue in API 18+:
<一个href="https://$c$c.google.com/p/android/issues/detail?id=59368">https://$c$c.google.com/p/android/issues/detail?id=59368<一href="https://$c$c.google.com/p/android/issues/detail?id=59700">https://$c$c.google.com/p/android/issues/detail?id=59700
这个问题似乎当一个TextView是一个可滚动容器(例如ListView控件)的组成部分,使视图忽略出于某种原因,垂直重力(一些消息来源认为这有做的TextView的是一个RelativeLayout的儿童发生,虽然这是我的经验是不涉及这样的布局,即使会发生这种情况)。
The problem seems to occur when a TextView is part of a scrollable container (e.g. ListView), making the view ignore the vertical gravity for some reason (some sources suggest this has to do with the TextView being the child of a RelativeLayout, though it's been my experience that this can happen even when no such layout is involved).
一个可能的解决方法(虽然不是一个特别优雅的),将是包装在一个LinearLayout中的TextView的。然后,您可以使用layout_gravity上的TextView到的LinearLayout内线中锋,而不是依赖地心引力吧,(只要确保WRAP_CONTENT因此文本本身是否正确居中)。
A possible workaround (albeit not a particularly elegant one), would be to wrap the TextView in a LinearLayout. You can then use "layout_gravity" on the TextView to center it inside the LinearLayout, instead of relying on "gravity" (just make sure to wrap_content so the text itself is properly centered).
例如,在你的例子:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/seekbar"
android:layout_toLeftOf="@+id/seekbar"
android:layout_marginLeft="@dimen/margin_tiny_double" >
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingRight="@dimen/margin_tiny"
android:text="@string/text1"
android:textColor="@color/black"
android:textSize="@dimen/size_text_normal" />
</LinearLayout>
此方法确实有添加,否则,不必要的水平视图层次结构的缺点,但它目前似乎是解决这个问题的唯一途径(不是恢复到一个较早的API级别等)。
This method does have the disadvantage of adding an otherwise-unnecessary level to your view hierarchy, but it currently seems to be the only way around this (other than reverting to an earlier API level).
也看到类似的问题:Android SDK 18 TextView的重力不起作用垂直
Also see similar question at:Android sdk 18 TextView gravity doesn't work vertically
这篇关于机器人:在API重未能18+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!