2015-05-27 52 views
1

我對Android編程非常陌生。我只是想創建一個非常簡單的標籤佈局。我希望將選項卡上的圖像放置在文本上方,但這不會發生。圖像最終放置在文字上,看起來很糟糕。我一直堅持這幾天。TabHost文本位置

任何幫助,非常感謝。

謝謝

<TabHost 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tabHost" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:backgroundTint="#ff5c2dff" 
       android:baselineAligned="true" 
       android:measureWithLargestChild="true" 
       android:minHeight="50dp"> 


      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:minHeight="50dp"> 

       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="New Button" 
        android:id="@+id/button" 
        android:layout_gravity="center_horizontal" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:minHeight="50dp"></LinearLayout> 
     </FrameLayout> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:background="#ffff2a1d"></TabWidget> 
    </LinearLayout> 
</TabHost> 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getSupportActionBar().hide(); 
    setContentView(R.layout.activity_main); 


    TabHost tabHost=(TabHost)findViewById(R.id.tabHost); 
    tabHost.setup(); 

    TabSpec spec1=tabHost.newTabSpec("Tab 1"); 
    spec1.setIndicator("Tab 1"); 
    spec1.setContent(R.id.tab1); 


    TabSpec spec2=tabHost.newTabSpec("Tab 2"); 
    spec2.setIndicator("Tab 2"); 
    spec2.setContent(R.id.tab2); 

    TabSpec spec3=tabHost.newTabSpec("Tab 3"); 
    spec3.setIndicator("Tab 3"); 
    spec3.setContent(R.id.tab3); 

    tabHost.addTab(spec1); 
    tabHost.addTab(spec2); 
    tabHost.addTab(spec3); 

    setTabIcon(tabHost, 0, R.drawable.homeb); 
} 


public void setTabIcon(TabHost tabHost, int tabIndex, int iconResource) { 
    ImageView tabImageView = (ImageView) tabHost.getTabWidget().getChildTabViewAt(tabIndex).findViewById(android.R.id.icon); 
    tabImageView.setPadding(10, 10, 10, 15); 
    tabImageView.setVisibility(View.VISIBLE); 

    tabImageView.setBackgroundColor(Color.BLUE); 
    tabImageView.setColorFilter(Color.GREEN); 
    tabImageView.setImageResource(iconResource); 



} 
+0

我真的會建議使用標籤庫。有很多很棒的選項讓選項卡變得更容易。我還建議不要在選項卡上放置圖像。以下是製表符的最新指南。如果你想要一個例子,我可以發佈它。 http://www.google.com/design/spec/components/tabs.html –

+0

謝謝,但我想我的Android應用程序匹配我的iOS應用程序。 –

+0

我明白。僅僅想到,Android用戶可能並不熟悉IOS設計,因此不便於用戶使用。 –

回答

0

1)創建一個佈局和設計它作爲含有兩個元素的垂直定向的LinearLayout - 的ImageView和TextView的(給出填充/保證金等,根據您的要求)。
2)現在在您定義TabSpec的現有代碼中 - 將上面的佈局設置爲其指標。現在使用它們的ID更新ImageView和TextView。
你完成了。
我會粘貼你的代碼,但它不允許

+0

代碼肯定會有所幫助。就像我說的我是一個初學者,我不知道如何做到這一點。 –