2012-03-01 3 views
4

.setDividerDrawable()에 문제가 있습니다. 아이스크림 샌드위치보다 낮은 버전에서만 작동합니다. 에뮬레이터를 실행하면 탭이 완벽하게 표시되지만 구분선은 표시되지 않습니다. 더 낮은 버전의 안드로이드를 에뮬레이트 할 때 아무런 문제가 없다.ICS에 탭 분할자가 표시되지 않음

이 코드를 사용하여 TabHost를 만듭니다. 나는 이것이 ICS가 움츠 리게 만드는 원인에 대한 단서가 없다. enter image description here 단지 1 픽셀 폭 .jpg 파일이다

manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.sbl.mytabapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:debuggable="true" > 
     <activity 
      android:name=".MyTabApp" 
      android:label="@string/app_name" 
      android:theme="@style/MyTabAppTheme" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Page1"></activity> 
     <activity android:name=".Page2"></activity> 
     <activity android:name=".Page3"></activity> 
    </application> 

</manifest> 

MyTabApp.java (R.drawable.divider)이 이미지를 말한다. ICS에서는 표시되지 않습니다.

public class MyTabApp extends TabActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 

     tabHost.getTabWidget().setDividerDrawable(R.drawable.divider); 

     intent = new Intent().setClass(this, Page1.class); 
     spec = tabHost.newTabSpec("page1").setIndicator(getLayoutInflater().inflate(R.layout.tab1, null)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, Page2.class); 
     spec = tabHost.newTabSpec("page2").setIndicator(getLayoutInflater().inflate(R.layout.tab2, null)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, Page3.class); 
     spec = tabHost.newTabSpec("page3").setIndicator(getLayoutInflater().inflate(R.layout.tab3, null)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 

    } 
} 

main.xml에 tab1.xml이 tab2.xml는, tab3.xml 모두 같은 참조 스타일을 포함

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<style name="MyTabAppTheme" parent="android:style/Theme"> 
    <item name="android:windowNoTitle">true</item> 
</style> 


<style name="tablayout" parent="android:style/Theme"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
    <item name="android:height">48dp</item> 
    <item name="android:gravity">center</item> 
    <item name="android:textColor">@color/font</item> 
    <item name="android:background">@drawable/tabselector</item> 
</style> 

<style name="contentlayout" parent="android:style/Theme"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
    <item name="android:textColor">@color/font</item> 
    <item name="android:background">@color/background</item> 
</style> 
</resources> 

<?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="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 
</TabHost> 

style.xml. 탭 1 :

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tab1" 
    style="@style/tablayout" /> 

tabselector.xml 탭 드로어 블 배경은 9patch 배경 이미지입니다.

<?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/normal" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/selected" /> 

    <!-- Focused states --> 
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/normal_focused" /> 
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/selected_focused" /> 

    <!-- Pressed --> 
    <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/normal_pressed" /> 
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/selected_pressed" /> 
</selector> 

모양 : 탭 배경은 9 패치 배경 이미지입니다.
enter image description here

+0

정보가 충분하지 않습니다. R.drawable.divider 란 무엇입니까? 탭 표시기보기 및 탭 자체의 레이아웃 XML은 어디에 있습니까? –

+0

알겠습니다. 이해합니다. 더 많은 정보를 추가했습니다. 희망이 도움이됩니다. – Simon

답변

4

나는 동일한 문제가 있었고, 마침내 수동으로 구분 기호를 추가했습니다. 탭 사이에 ImageView 추가하기.

public class MyTabApp extends TabActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView divider = new ImageView(this); 
     divider.setImageResource(R.drawable.tab_seperator); 

     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 

     intent = new Intent().setClass(this, Page1.class); 
     spec = tabHost.newTabSpec("page1").setIndicator(getLayoutInflater().inflate(R.layout.tab1, null)) 
        .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.getTabWidget().addView(divider, LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); 

     intent = new Intent().setClass(this, Page2.class); 
     spec = tabHost.newTabSpec("page2").setIndicator(getLayoutInflater().inflate(R.layout.tab2, null)) 
        .setContent(intent); 
     tabHost.addTab(spec); 
    } 
} 
+0

나는이 오류를보고 있는데, 수정을 도울 수 있나 : E/AndroidRuntime (683) : java.lang.RuntimeException : 활동을 시작할 수 없습니다. ComponentInfo {com.sbl.mytabapp/com.sbl.mytabapp.MyTabApp} : java.lang.IllegalStateException : 지정된 자식에 이미 상위 항목이 있습니다. 먼저 부모의 부모에 대해 removeView()를 호출해야합니다. – Simon

+1

분할 자의 동일한 인스턴스를 여러 번 추가하려고합니까? 보기 (또는 ImageView) 인스턴스는 한 번만 추가 할 수 있습니다. 다중 분할자를 필요로하는 경우 다중 인스턴스를 작성해야합니다. –

+1

Spot on. 그러나 다른 드로어 블을 사용하여 비슷한 뷰를 추가하려고하면 뷰가 엉망이됩니다. tab1과 tab2 만 누를 수 있습니다. Tab2와 tab3은 둘 다 tab2와 같이 작동합니다. 나는 그것이 그 견해와 관련이 있다고 생각한다. 이 붙여 넣기 내 코드를 보여줍니다 : [링크] (http://pastebin.com/z2PDRD8k) 21 및 28 줄 구분 기호를 추가했습니다. – Simon

1

이 동작은 ICS 탭 테마의 기본 동작으로 보이며 해결 방법이 필요합니다. this answer, 특히 @Janusz가 제공 한 OP를 살펴보십시오.

+0

나는 이것 역시보고 있었지만 이것이 실제로 해결책이긴하지만 사전 ICS 탭을 모방하지는 않는다. 100 % – Simon

1

이 코드를 사용해보십시오!

if(Build.VERSION.SDK_INT >= 11) 
    mTabHost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE); 
관련 문제