2014-10-24 4 views
1

누군가 센터 탭에서 어떻게 이미지를 설정했는지 알 수 있습니까? xml을 변경하려고했지만 작동하지 않았습니다. 내 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"> 

    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true"/> 
    </RelativeLayout> 
</TabHost> 

내 코드의 활동 아래 :

@SuppressWarnings("deprecation") 
     final 
     TabHost tabHost = getTabHost(); 

     // Tab 1 
     TabSpec aba1spec = tabHost.newTabSpec("Tab 1"); 
     // setting Title and Icon for the Tab 
     tabHost.getTabWidget().setStripEnabled(false); 
     aba1spec.setIndicator("",getResources().getDrawable(R.drawable.tab1)); 
     Intent tab1 = new Intent(this, tab1.class); 
     aba1spec.setContent(tab1); 

     // Adding all TabSpec to TabHost 
     tabHost.addTab(aba1spec); // Adding tab1 

이미지 :

enter image description here

고마워요.

답변

2

1) - my_xml.xml 또는 원하는 이름으로 레이아웃 파일을 만듭니다.

이 코드는 report_tabs에서 사용합니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layout_tabsLayout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/btn_selector" 
android:gravity="center" 
android:orientation="vertical" 
> 

<ImageView 
android:id="@+id/img_icon" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:layout_marginTop="3dp" 
/> 
</LinearLayout> 

2) - 귀하의 활동에 아래 코드를 사용하십시오.

Intent intent; 
tabhost = getTabHost(); 
TabHost.TabSpec tabspec; 
intent = new Intent().setClass(getApplicationContext(),xxxxx.class); 

tabspec = tabhost.newTabSpec("First"); 
view = LayoutInflater.from(this).inflate(R.layout.report_tabs, 
tabhost.getTabWidget(), false); 
imgtabF = (ImageView) view.findViewById(R.id.img_icon); 
imgtabF.setBackgroundResource(R.drawable.tab_icon_selector); 

tabspec.setIndicator(view); 
tabspec.setContent(intent); 
tabhost.addTab(tabspec); 

3) -이 같은 탭을 클릭의 아이콘을 변경 당김의 이름 tab_icon_selector을 가진 파일을 만듭니다 -

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Non focused states --> 
<item android:state_focused="false" android:state_selected="false" 
     android:state_pressed="false" android:drawable="@drawable/medical_icon_unselect"  
/> 
<item android:state_focused="false" android:state_selected="true" 
android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
<!-- Focused states --> 
<item android:state_focused="true" android:state_selected="false" 
android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
<item android:state_focused="true" android:state_selected="true" 
android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
<!-- Pressed --> 
<item android:state_pressed="true" android:drawable="@android:color/transparent"/> 
</selector> 

지금 당신은 당신의 사용자 정의 탭 표시 줄을 만들 수 있습니다 및 이미지 아이콘이 될 것입니다 탭 중심

source

:

+0

내가 내 게시물 메드을 편집, 난 내 활동에 예를 적용하는 방법을 이해하지 않습니다. – lfrancatto

+0

어떤 부분을 얻지 못하셨습니까? –

+0

부품 번호 2. 나는 탭을 위해 특정한 활동을 하나 생성해야합니까? 내 게시물이 같은가요? – lfrancatto