2015-04-28 175 views
0

我自定義了我的Android應用中選項卡活動的標籤佈局。問題是如何垂直居中。文本自動水平居中。我試着寫gravity:center,layout_gravity:center。更:我試着在我TextView寫確切的高度,但沒有任何工程(其實確切像素的工作很好,但是當我改變設備的方向 - 文本沒有集中面向如何垂直居中標籤文本

這是我的標籤佈局代碼:

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

    <TextView 
     android:id="@+id/tab_name" 
     android:paddingTop="11dp" 
     android:paddingBottom="11dp" 
     android:layout_width="match_parent" 
     android:textSize="14dp" 
     android:textStyle="bold" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:maxLines="1" 
     android:layout_gravity="center" /> 

</LinearLayout> 

這裏是我的問題的截圖: Screenshot Screenshot-2

+1

我不完全確定發生了什麼問題。你能發佈一些可能有用的圖片嗎?還有什麼是在dp的textview的高度和寬度? – ngoa

+0

你想垂直居中文本或小部件? – Josef

+0

@ngoa我更新了與我的問題的圖片上的鏈接帖子 –

回答

1

你只需要添加android:gravity="center"LinearLayout
使用的layout_widthlayout_height的。

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

    <TextView 
     android:id="@+id/tab_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="11dp" 
     android:paddingBottom="11dp" 
     android:textSize="14dp" 
     android:textStyle="bold" 
     android:maxLines="1" /> 

</LinearLayout> 
+0

不幸的是,這並不適用於我..我更新了我的帖子,鏈接到我的問題的屏幕截圖(代碼中,您在答案中給出的代碼在那裏應用) –

+0

請在開發人員選項中啓用「顯示佈局邊界」發佈額外的屏幕截圖:) – ByteHamster

+0

我已將屏幕截圖添加到帖子 –