2013-05-02 48 views
0
<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

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

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="0dip" 
      android:layout_marginRight="0dip" > 

      <TextView 
       android:id="@+id/view_all" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:tag="tab0" 
       android:text="View All" /> 

      <TextView 
       android:id="@+id/favourite" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:tag="tab1" 
       android:text="Favourite" /> 

      <TextView 
       android:id="@+id/make_fav" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:tag="tab2" 
       android:background="@drawable/prev_button" /> 
     </TabWidget> 

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

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

尝试在运行时通过在TabHost的onTabChanged()上使用以下代码更改textview(make_fav)图像。基本上我想在用户点击标签时改变图像。图像在tabhost中运行时在textview中拉伸

LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
tabHost.getCurrentTabView().setBackgroundResource(R.drawable.prev_button); 
tabHost.getCurrentTabView().setLayoutParams(params); 

问:我怎样才能获得9修补图像不拉伸,即使它正确显示在XML?

+0

使用选择器在运行时更改图像 – juned 2013-05-02 13:13:21

回答

0

像下面

tab_image.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/tab_pressed_image" /> 
    <item android:state_focused="true" 
     android:drawable="@drawable/tab_pressed_image" /> 
    <item 
     android:drawable="@drawable/simple_image" /> 
</selector> 

把这个XML绘制目录下,刚刚成立这个形象片的背景创建XML。

+0

嘿感谢您的解决方案。但图片仍然在标签中拉伸并占据整个标签空间。 – akshay 2013-05-03 04:46:23

+0

因此,您的图像应该存在问题,您是否具有不同分辨率的密度特定图像? – juned 2013-05-03 10:08:03

+0

是的,我使用不同分辨率的密度特定图像。还是行不通 – akshay 2013-05-08 13:19:27