Android sdk 18中的TextView垂直方向上的gravity属性不起作用

4
我有一个关于SDK 18的问题。 我的文本视图的重力在垂直方向上不起作用。但是在SDK17上可以工作。
图片和代码:
header_of_listView.xml:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/iv_bg"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/entete.size.h"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="true"
            android:contentDescription="@string/blank"
            android:scaleType="fitXY"
            android:src="@drawable/cellule_flecher_mise_en_valeur" />

        <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/iv_bg"
            android:layout_alignLeft="@id/iv_bg"
            android:layout_alignRight="@id/iv_bg"
            android:layout_alignTop="@id/iv_bg"
            android:layout_marginBottom="6dp"
            android:gravity="center"
            android:text="@string/a_remplacer"
            android:textColor="@color/tableviewText"
            android:textSize="18sp"
            android:textStyle="bold" 
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

AndroidManifest.xml

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

sdk = 17

AndroidManifest.xml

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

sdk = 18

使用黑色背景的TextView android:background="#000000"

sdk 18 with black background

如果有人有想法。 我已经尝试将wrap_content更改为match_parent。

唯一有效的方法是在TextView上使用android:layout_height="@dimen/entete.size.h",但是边距不适用。 目前我将项目保留到SDK 17。


移除 android:targetSdkVersion="18" 并运行... - Looking Forward
可以工作。但是我收到了警告:“标记应该使用android:targetSdkVersion =“?”指定目标API级别(最高验证版本;在运行更高版本时,可能启用兼容性行为)。” - Anthone
只要您指定了最小SDK版本,这就不会成为问题。 - Looking Forward
我也遇到了完全相同的错误。你找到任何解决方案了吗?而不是将targetSdk设置为17。 - Informatic0re
也就是说,对于高度来说,使用match_parent没有效果,视图仍然会被包裹。 - Informatic0re
我再次检查了一下:问题不在TextView上,而是在ListView/GridView上!!!如果我将此TextView移动到List/Grid-View之外的另一个RelativLayout中,它看起来就像应该的样子... - Informatic0re
3个回答

9
问题出在将RelativLayout作为ListView项的根布局。如果你在RelativeLayout周围加上LinearLayout,它也可以在targetSdk = 19下工作。GridView也会出现同样的问题。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   ... your layout like before

</LinearLayout>

这不是一个很好的解决方法,但比强制使用targetSdk = 17要好一些。


我和原帖作者遇到了同样的问题,但是我的TextView位于RecyclerView中的CardView内部。将整个CardView布局包装在match_parent LinearLayout中解决了这个问题。非常感谢! - Flyview
我错了,那对我没有起作用。卡片的宽度开始与父元素一样宽,这不是我想要的。我不得不在 CardView 元素中包装一个 RelativeLayout 中的无用 LinearLayout。 - Flyview

0

我曾经遇到过同样的问题,而且只在Android 19上出现。我将目标API设置为23,问题得到了解决。

我发现给TextView设置高度可以解决这个问题。

具体来说,代码如下:

         <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/iv_bg"
            android:layout_alignLeft="@id/iv_bg"
            android:layout_alignRight="@id/iv_bg"
            android:layout_alignTop="@id/iv_bg"
            android:layout_marginBottom="6dp"
            android:gravity="center"
            android:text="@string/a_remplacer"
            android:textColor="@color/tableviewText"
            android:textSize="18sp"
            android:textStyle="bold" 
            android:layout_centerHorizontal="true"/>

我是这样做的:
         <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="40dp"            <!-- this is the key-->      
            android:layout_alignBottom="@id/iv_bg"
            android:layout_alignLeft="@id/iv_bg"
            android:layout_alignRight="@id/iv_bg"
            android:layout_alignTop="@id/iv_bg"
            android:layout_marginBottom="6dp"
            android:gravity="center"
            android:text="@string/a_remplacer"
            android:textColor="@color/tableviewText"
            android:textSize="18sp"
            android:textStyle="bold" 
            android:layout_centerHorizontal="true"/>

出于性能问题,我不想添加另一个线性布局。


-4

移除 android:targetSdkVersion="18" 然后运行。


我失去了平板电脑的兼容性。 - Anthone
6
这太疯狂了...抱歉,但这个回答是如此之错误,它不配被称为一个答案! - WarrenFaith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接