2010-08-21 6 views
6

안드로이드에서 더 작은 탭을 만들려고합니다. 그러나 더 작은 탭을 만들 때 발생하는 모든 것이 더 큰 탭을 보여주기 때문에 작동하지 않을 수 있습니다. 드로어 블없이.안드로이드에서 더 작은 탭 만들기

이것은 내 탭에 대한 레이아웃 코드입니다.하지만 높이가 어떤 이유로 든 포장되지 않습니다. 단지 Android의 일반적인 레이아웃 높이로 이동합니다.

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

누군가가 나 페이스 북 응용 프로그램 같은 것을 만들 수 있도록 할 수 있다면 그것은 좋은 것입니다 - 나는 그게 정말 깨끗하게 보이는 생각하고 내가 같은 것을 구현 싶어요 :

답변

7

그럼 여기까지였다을 나는 ... 그것은 그럼에도 불구하고,이 당신에게 당신이 원하는 모양의 기본 구현을 얻어야한다, 수 만한다고 생각

TabHost    host  = getTabHost(); 
TabSpec    spec  = null; 
TextView   tab1  = null, 
        tab2  = null; 
Intent    intent  = null; 
Resources   resources = getResources(); 
XmlResourceParser parser  = null; 
ColorStateList  text  = null; 
StateListDrawable[] drawables = new StateListDrawable[2]; 
int[]    selected = {STATE_SELECTED}, 
        unselected = {STATE_UNSELECTED}; 
Color    selectedColor = Color.argb(255, 255, 255, 255), 
        defaultColor = Color.argb(255, 119, 119, 119); 

// Load the colour lists. 
parser = resources.getXml(R.color.tab_text); 
text = ColorStateList.createFromXml(getResources(), parser); 

// Add an initial tab. 
...Create Tab Contents Here... 
spec = host.newTabSpec("tab1"); 
tab1 = new TextView(this); 
tab1.setText(R.string.all_tab_title); 
tab1.setGravity(android.view.Gravity.CENTER); 
tab1.setTextSize(18.0f); 
tab1.setTextColor(text); 
spec.setIndicator(tab1); 
spec.setContent(intent); 
host.addTab(spec); 

// Add a second tab. 
...Create Tab Contents Here... 
spec = host.newTabSpec("tab2"); 
tab2 = new TextView(this); 
tab2.setText(R.string.category_tab_title); 
tab2.setGravity(android.view.Gravity.CENTER); 
tab2.setTextSize(18.0f); 
tab2.setTextColor(text); 
spec.setIndicator(tab2); 
spec.setContent(intent); 
host.addTab(spec); 

// Set the background drawable for the tabs and select the first tab. 
drawables[0] = new StateListDrawable(); 
drawables[0].addState(selected, new ColorDrawable(selectedColor)); 
drawables[0].addState(unselected, new ColorDrawable(defaultColor)); 
drawables[1] = new StateListDrawable(); 
drawables[1].addState(selected, new ColorDrawable(selectedColor)); 
drawables[1].addState(unselected, new ColorDrawable(defaultColor)); 
tab1.setBackgroundDrawable(drawables[0]); 
tab2.setBackgroundDrawable(drawables[1]); 
host.setCurrentTab(0); 

이 탭 경계 또는 간격 betwee을 고려하지 않습니다보다 더 복잡 n 요소. 또한 ./res/color 디렉토리에서 다음과 같은 색상 상태 목록 정의가 필요합니다.

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:color="#ff000000" /> 
    <item android:state_selected="false" android:color="#ffaaaaaa" /> 
    <item android:color="#ffffffff"/> 
</selector> 

희망이 있습니다.

+0

와우, 이것은 내가 찾고있는 것보다 훨씬 더 많습니다. 누군가가 더 확실한 방법을 게시 할 때까지이 대답을 받아 들일 것입니다. – hwrdprkns

+0

STATE_SELECTED 및 STATE_UNSELECTED는 (는) 무엇에 대한 ID입니까? – pakore

+1

@pakore - 이러한 정의를 무시한 것에 대해 사과드립니다. STATE_SELECTED는 android.R.attr.state_selected와 같은 것으로 정의됩니다. STATE_UNSELECTED는 STATE_SELECTED * -1과 같습니다. – Woody

3

다른 포럼에서 이것을 보았지만 여기에 전달했습니다.


TabHost th = getTabHost(); 
.... 
// Setup all the tabs -- in my case, with text only -- no icons 
.... 
int iCnt = th.getTabWidget().getChildCount(); 
for(int i=0; i&ltiCnt; i++) 
    th.getTabWidget().getChildAt(i).getLayoutParams().height /= 2; // Or the size desired 
+0

나는 이것을 시도하고 작동합니다. – Federico

관련 문제