2016-04-28 122 views
-1

編輯:對不起,這是我的實施中的一個問題,佈局權重不加1,因此問題。它在我修復layoutWeight之後起作用。Android:文本不在textView中居中?

我不知道爲什麼,但我無法在此custom TextView裏面居中文字,請有人請幫忙。我嘗試了各種各樣的東西,但都沒有工作。 IconTextView內的圖標更靠近右側,而不是中心。謝謝 !!

<LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.25"> 

      <com.malinskiy.materialicons.widget.IconTextView 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@drawable/ripple_effect" 
        android:gravity="center" 
        android:text="{zmdi-play}" 
        android:textColor="#ffffff" 
        android:textSize="40dp" 
        app:hacky_preview="true" /> 
    </LinearLayout> 
+0

分享屏幕截圖。 –

回答

0

嘗試

android:textAlignment="gravity" 

添加到您的IconTextView元素

0

這不是一個妥善的解決辦法,但是,因爲也許問題是在IconTextView的實現,你可以嘗試設置IconTextView與wrap_content並將整個IconTextView集中在包裝LinearLayout中。事情是這樣的:

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="0.25" 
    android:gravity="center"> 

    <com.malinskiy.materialicons.widget.IconTextView 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:background="@drawable/ripple_effect" 
      android:text="{zmdi-play}" 
      android:textColor="#ffffff" 
      android:textSize="40dp" 
      app:hacky_preview="true" /> 
</LinearLayout> 
0

可以使用文本視圖相對相對佈局如下面的代碼

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
> 

<com.malinskiy.materialicons.widget.IconTextView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:background="@drawable/ripple_effect" 
     android:text="{zmdi-play}" 
     android:textColor="#ffffff" 
     android:textSize="40dp" 
     app:hacky_preview="true" /> 
</RelativeLayout> 

希望這將幫助你

+0

不能這樣做,我正在使用佈局權重 – varunkr

+0

您可以在線性佈局中添加相對佈局,然後可以將線寬分配給線性佈局。 – sukhbir

0
<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.malinskiy.materialicons.widget.IconTextView 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/ripple_effect" 
       android:gravity="center" 
       android:text="{zmdi-play}" 
       android:textColor="#ffffff" 
       android:textSize="40dp" 
       app:hacky_preview="true" /> 
</LinearLayout> 

試試這個..可以幫助你..

+0

謝謝,請參閱編輯。 – varunkr