简体   繁体   English

Android sdk 18 TextView引力不能垂直工作

[英]Android sdk 18 TextView gravity doesn't work vertically

I have a probleme with the SDK 18. The gravity of my text view doesn't work vertically. 我对SDK 18有疑问。我的文本视图的重力不能垂直工作。 But it's working with sdk17. 但它正在与sdk17合作。

Pictures and code : 图片和代码:

header_of_listView.xml : 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 AndroidManifest.xml中

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

sdk = 17

AndroidManifest.xml AndroidManifest.xml中

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

sdk = 18

with black background for textview android:background="#000000" 用textview android的黑色背景:background =“#000000”

sdk 18与黑色背景

If someone have an idea. 如果有人有想法。 I have try to change wrap_content with match_parent. 我试着用match_parent改变wrap_content。

The only thing that work is android:layout_height="@dimen/entete.size.h" on the TextView, but the margin doesn't apply. 唯一可行的是android:layout_height =“@ dimen / entete.size.h”在TextView上,但保证金不适用。 For the moment I leave the project to SDK 17. 目前我将项目留给SDK 17。

The problem is the RelativLayout as root layout of the listview item. 问题是RelativLayout作为listview项的根布局。 If your put a LinearLayout around the RelativeLayout it works also with targetSdk = 19. Same problem appears with the GridView. 如果你在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>

This is not really a nice workaround - but better than put targetSdk = 17 I guess. 这不是一个很好的解决方法 - 但我认为比使用targetSdk = 17更好。

I had the same problem and it happened only in Android 19. I target api 23 and it works fine. 我有同样的问题,它只发生在Android 19.我的目标api 23,它工作正常。

I found out that giving a height to the textview fixed the problem 我发现给textview一个高度可以解决这个问题

It was like this: 就像这样:

         <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"/>

I made it like this: 我这样做了:

         <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"/>

I didn't want to add another linear layout because of performance issues. 由于性能问题,我不想添加另一个线性布局。

删除android:targetSdkVersion="18"并运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM