2016-10-23 75 views
-2

所以我有一個TextView中的文本居中問題。我想要將文本放置在我的Textview的中心。這裏爲f我的代碼:Textview居中文本

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="#EAEAEA" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:weightSum="3"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/Ciech" 
     android:src="@drawable/ciech" 
     android:layout_marginTop="16dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp"/> 
</LinearLayout> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@drawable/border" 
    android:layout_margin="5dp"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TextView 

      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="sampletext" 
      android:layout_margin="16dp"/> 
    </ScrollView> 
</LinearLayout> 


    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 
     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="16dp"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/globus" 
       android:src="@drawable/grid_world" 
       /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="sampletext" 
       android:textColor="#000000" 
       android:textStyle="bold" 
       android:layout_alignBottom="@+id/globus" 
       android:layout_toEndOf="@+id/globus" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/globus" 
       android:gravity="center" /> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/koperta" 
       android:src="@drawable/close_envelope" 
       android:layout_below="@+id/globus"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="sampletext" 
       android:textColor="#000000" 
       android:textStyle="bold" 
       android:layout_alignBottom="@+id/koperta" 
       android:layout_alignTop="@+id/koperta" 
       android:layout_toRightOf="@+id/koperta" 
       android:gravity="center" /> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/torba" 
       android:src="@drawable/portfolio_black_tool" 
       android:layout_below="@+id/koperta"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="sampletext" 
       android:textColor="#000000" 
       android:textStyle="bold" 
       android:layout_alignBottom="@+id/torba" 
       android:layout_toEndOf="@+id/torba" 
       android:layout_alignTop="@+id/torba" 
       android:layout_toRightOf="@+id/torba" 
       android:gravity="center" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/znaczek" 
       android:src="@drawable/map_marker" 
       android:layout_below="@+id/torba"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="sampletext" 
       android:textColor="#000000" 
       android:textStyle="bold" 
       android:layout_alignBottom="@+id/znaczek" 
       android:layout_toEndOf="@+id/znaczek" 
       android:layout_alignTop="@+id/znaczek" 
       android:layout_toRightOf="@+id/znaczek" 
       android:gravity="center" /> 

     </RelativeLayout> 
     </ScrollView> 
    </LinearLayout> 

</LinearLayout> 

當我看到Android Studio中預覽它顯示我: this 而且這正是我想要的。然而,當我運行模擬器,然後它突然改變了,看起來像:this

有人可以幫我這個人,我感到困惑,

+0

你可以發佈完整的XML? –

+0

添加textview屬性:'android:layout_gravity =「center_vertical」' – pRaNaY

+0

android:layout_gravity =「center_vertical」不會改變任何東西。我發佈了我的全部xml –

回答