2017-02-14 91 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>