2012-11-28 50 views
0

我試圖創建一個標籤主機,如所附的屏幕截圖所示,但與圖標圖像和文本對齊問題 - 圖像需要在文本上方對齊,但所有嘗試失敗;Android的 - 與tabhost和對齊imageview和textview的問題

PM For Image if unable to see link here

我已經包含了一些示例代碼,但林無法強制文本的圖像頂部對齊 - 它只是如果我增加layout_height在tab_indicator文件大於66dip但是我在有限的工作原理空間和提供的圖像。如果你不能看到圖像/鏈接,圖像不會被強制在文本上方,所以圖像的一部分在文本後面!

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="88.0dip" 
    android:layout_height="66.0dip" 
    android:layout_weight="1.0"> 

    <include layout="@layout/tab_indicator_portrait" /> 

</FrameLayout> 

tabindicator_protrait.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tab_indicator_portrait" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingBottom="3.0dip" 
    android:paddingTop="3.0dip" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|center" 
     android:layout_marginTop="3.0dip" /> 

    <TextView 
     android:id="@+id/title" 
     style="?android:attr/tabWidgetStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center" 
     android:textColor="@color/tab_indicator_text" 
     android:textSize="12.0dip" /> 

</FrameLayout> 

tab_host.xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/topLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="0.0dip" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="0.0dip" 
      android:layout_weight="1.0" /> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dip" 
      android:padding="0dip" /> 
    </LinearLayout> 

</TabHost> 

有幾個原因,我得到了它的設置是這樣;

  • 的tab_indicator.xml文件還可以包含一個景觀文件,如果我需要處理不同的景觀,即未來的版本根據市場反饋/測試
  • 允許應用程序有一個通用的外觀和感覺,因爲我只想要當用戶在標籤間移動時改變的圖標 - 在4.0.4中有tabhost的問題,這看起來像是一種更清晰的處理方式。

我試過一些代碼在我的TabActivity來處理基於屏幕大小的佈局大小;

private void detectScreen() 
{ 
    Display localDisplay = getWindowManager().getDefaultDisplay(); 

    if (localDisplay.getWidth() < localDisplay.getHeight()) 
    { 
     int dips = 45; 
     TabHost localTabHost = getTabHost(); 
     DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics(); 

     for (int i = 0; i < localTabHost.getTabWidget().getTabCount(); i++) 
     { 
      View localView = localTabHost.getTabWidget().getChildTabViewAt(i); 
      localView.getLayoutParams().height = ImageUtils.convertDIPToPixels(localDisplayMetrics, (float)dips); 
     } 
    } 
} 

但同樣,改變佈局的大小確實使圖像變小,但不與文本對齊。

清單文件還包含以下內容;

<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/> 
+0

我測試過的tab_indicator_protait.xml改變的FrameLayout到的LinearLayout - 這只是刪除了文本和顯示圖像! – user1859465

回答

0

更改指標的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="120dip" 
    android:layout_height="64dip" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="30dip" 
     android:layout_height="30dip" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/cntDesc" /> 

    <TextView 
     android:id="@+id/title" 
     style="?android:attr/tabWidgetStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

這已經正常工作對我來說

+0

這工作,但並不想手動設置寬度/高度。我想我需要編輯圖像和9補丁設置。儘管感謝您的幫助。 – user1859465