2012-08-08 96 views
0

我在我的應用程序中有一個TabHost,並試圖將圖像instad文本。 我的問題是,圖像拉伸到整個屏幕,而不是其實際大小。即時通訊使用「wrap_content」在高度和寬度,它仍然在做。當我把相同的圖像與「wrap_content」一些其他的東西,它確定。只有在選項卡中它很奇怪。這就是它的樣子:在標籤中拉伸的圖像android

enter image description here

,而不是尋找這樣的:

enter image description here

這就是我的代碼:

<TabHost 
    android:id="@+id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="wrap_content" 
       android:layout_height="60dp" 
       android:gravity="center_vertical" 
       android:showDividers="none" > 

      </TabWidget> 

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

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="start" /> 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="stop" /> 

       <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="first tab" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="second tab B" /> 

       <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Secont tab" /> 

      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

謝謝!

回答

2

你可以嘗試從這樣的java代碼縮放你的圖像視圖;

yourImageView.setScaleType(ScaleType.CENTER); 

或者像這樣的XML;

android:scaleType="fitCenter" 
1

我固定它使用相對佈局,像這樣:

<TabHost 
    android:id="@+id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.8" > 

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.1" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="false" 
       android:layout_centerHorizontal="true" 
       android:gravity="center_vertical" 
       android:showDividers="none" > 
      </TabWidget> 

     </RelativeLayout> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.9" > 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="start" /> 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="stop" /> 

       <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="first tab" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="second tab B" /> 

       <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Secont tab" /> 

      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

不管怎樣,謝謝!!

1

很遲纔回答,我敢肯定你不在乎了,但別人可能會。

這裏是另一個Link這個

的九補丁生成器允許您快速生成簡單的九個補丁程序在不同屏幕密度,根據指定的源藝術品。

如果您對此有任何問題請看看Here。閱讀CommonsWare答案。