2017-02-14 99 views
0

當我將android:gravity應用於center(甚至textAlignment對於TextView爲true)時,即時更改將應用​​於當前活動(使用我的設備進行測試)並且文本居中。android:引力在TextView中不起作用

但是,當我安裝/重新啓動應用程序具有相同的更改(或回到當前活動從前一個),文本左對齊在TextView的開始。

的TextView試圖中心:機器人:ID =「@ + ID/ttamt

當我同時在活動運行的應用程序進行修改的文本只集中

實在無法理解自己做錯了什麼。我是在這裏做,

感謝您的時間和幫助

以下是我的佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/mainRel" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:background="#5f5f5f" 
     android:elevation="10dp" 
     android:transitionName="@string/transtrans"> 

     <ImageView 
      android:id="@+id/ttimg" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/com_facebook_tooltip_blue_topnub" 
      android:transitionName="timgTrans" /> 


     <TextView 
      android:id="@+id/ttamt" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:layout_below="@+id/ttimg" 
      android:layout_marginTop="20dp" 
      android:fontFamily="sans-serif" 
      android:gravity="center" 
      android:text="1001" 
      android:textColor="#fff" 
      android:textSize="55sp" 
      android:textStyle="bold|italic" 
      android:transitionName="tamtTrans" /> 


     <TextView 
      android:id="@+id/ttdate" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_below="@+id/ttamt" 
      android:layout_marginTop="25dp" 
      android:layout_toRightOf="@+id/timg" 
      android:fontFamily="sans-serif" 
      android:text="01/01/01" 
      android:textAlignment="center" 
      android:textColor="#fff" 
      android:textSize="22dp" 
      android:textStyle="bold|italic" /> 


    </RelativeLayout> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:background="#f0f0f0" 
     android:clickable="true" 
     android:elevation="15dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     app:cardCornerRadius="7dp"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/ttdesc" 
       android:layout_width="150dp" 
       android:layout_height="45dp" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="12dp" 
       android:fontFamily="sans-serif" 
       android:text="TestDesc" 
       android:textSize="16sp" 
       android:textStyle="bold|italic" 
       android:transitionName="tdescTrans" /> 

      <ImageView 
       android:layout_width="25dp" 
       android:layout_height="25dp" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="120dp" 
       android:src="@drawable/ic_mode_edit_black_24dp" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:background="#f0f0f0" 
     android:clickable="true" 
     android:elevation="15dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     app:cardCornerRadius="7dp"> 

     <TextView 
      android:id="@+id/ttmsg" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="20dp" 
      android:lineSpacingExtra="5dp" 
      android:text="View SMS" 
      android:textSize="15dp" /> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 
+0

文件 - > Invalidete緩存和重新啓動。有時android工作室錯誤並保留舊的更改。 (Build-> Rebuild項目也可能會訣竅) – X3Btel

+0

Build - > Clean或Re Build your project –

+0

沒有任何幫助,文本中心只發生在我進行更改後重新運行應用程序活動(即時更改)時加入'android:gravity =「center」')來呈現沒有'android:gravity =「center」'的代碼。 – user3820753

回答

0

看到this答案後,看起來像api 18或更高的問題。

做了同樣的事情,將我的視圖包裹在LinearLayout中並使用textAlignment =「center」。現在工作正常。

最終排版段:

<RelativeLayout 
    android:id="@+id/mainRel" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:background="#5f5f5f" 
    android:elevation="10dp" 
    android:transitionName="@string/transtrans"> 

    <ImageView 
     android:id="@+id/ttimg" 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="30dp" 
     android:src="@drawable/com_facebook_tooltip_blue_topnub" 
     android:transitionName="timgTrans" /> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="80dp" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/ttamt" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:layout_below="@+id/ttimg" 
      android:fontFamily="sans-serif" 
      android:text="1001" 
      android:textAlignment="center" 
      android:textColor="#fff" 
      android:textSize="55sp" 
      android:textStyle="bold|italic" 
      android:transitionName="tamtTrans" /> 

     <TextView 
      android:id="@+id/ttdate" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_below="@+id/ttamt" 
      android:layout_marginTop="25dp" 
      android:fontFamily="sans-serif" 
      android:text="01/01/01" 
      android:textAlignment="center" 
      android:textColor="#fff" 
      android:textSize="22dp" 
      android:textStyle="bold|italic" /> 

    </LinearLayout> 
</RelativeLayout>