2013-07-18 1 views
4

을 탭을 추가하고 아래의 스크린 샷에 부착 된 것처럼 현재의 일치 사용자에 탭 의존 동적 채팅을 추가 할 :어떻게 안드로이드 응용 프로그램에서 탭을 생성하고 (일치하는 사용자에 따라 다름) 동적 나는 안드로이드 채팅 응용 프로그램을 개발하고

enter image description here

In Screen shot 채팅 탭은 상단에 있지만 Chat Tabs at bottom을 원합니다. 지금은 세 일치하는 사용자가있는 경우 너무

후 3 개 탭,

이 네 일치하는 사용자가 다음 마찬가지로, 4 개 탭을 만들 수 있습니다 .. 경우

을 만드는 것이 onCreate method에서 논리를 개발하려는

나는 채팅 탭을 많이 검색했고 TabHost을 사용하여 채팅 탭을 만드는 방법을 찾았습니다.하지만 더 이상 사용되지 않습니다. 또 다른 방법은 Action Bar에 채팅 탭을 설정하는 것입니다.을 사용하는 것으로 나타났습니다. 나는 채팅 탭에 대해 무엇을 사용해야하는지 매우 혼란 스럽다.

도움이 될 것입니다.

답변

1
@SuppressWarnings("deprecation") 
public class MainActivity extends TabActivity { 
    public static TabHost tabHost; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.tab_main); 

     // call addtab() how many times you need and pass tag and image resource id   
    } 
    private void addTab(String tag, int drawableId) { 
     tabHost = getTabHost(); 

     TabHost.TabSpec spec = tabHost.newTabSpec(tag); 

     // tab_indicator layout contains only imageview. this is for fix image size, position 
     View tabIndicator = LayoutInflater.from(this).inflate(
       R.layout.tab_indicator, getTabWidget(), false); 
     ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
     icon.setImageResource(drawableId); 
     spec.setIndicator(tabIndicator); 
     tabHost.addTab(spec); 
    } 


} 
1

최신 버전의 안드로이드 포함 ActionBarSherlock 라이브러리. 그래서 당신은 직접 android에서 해당 라이브러리를 사용하여 탭을 추가 할 수 있습니다.

샘플 코드 :

try 
{ 
     ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     // For each of the sections in the app, add a tab to the action bar. 
     actionBar.addTab(actionBar.newTab().setText(R.string.firsttab).setTabListener(this)); 
     actionBar.addTab(actionBar.newTab().setText(R.string.second).setTabListener(this)); 
     actionBar.addTab(actionBar.newTab().setText(R.string.third).setTabListener(this)); 
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
1

지금은 잠시 동안 ActionBarSherlock의를 사용하고 있지만, 지금은 새로운 안드로이드 SDK (18) 작업 표시 줄 (호환)로 이주했다. 프로젝트는 액션 바 탭의 간단한 구현처럼 보입니다. 지금이 시점에서 ActionBarSherlock (또는 tabHost)을 사용해야하는 이유는 없습니다.

Action Bar Compat는 Action Bar Sherlock과 매우 유사하며 Android V4 지원 라이브러리 (SDK 7과 호환 가능)의 필수 구성 요소라는 이점이 있습니다. http://developer.android.com/tools/support-library/index.html의 "새 v7 appcompat 라이브러리"섹션을 참조하십시오.

또한 명확하게 문서화되는 이점이 있습니다. http://developer.android.com/tools/support-library/setup.html

, 당신은 지원 작업 표시 줄을 설정하려면이 가이드를 따르도록 할 데

(절 "자원 라이브러리 추가"에 특히주의) :이 가이드를 설정하는 방법에 대해 자세히 : http://developer.android.com/guide/topics/ui/actionbar.html

섹션 "탐색 탭 추가"에서는 tabListener에 대한 명확한 예제와 탭 추가 방법을 제공합니다. 추가 할 탭의 수를 결정하려면이 코드 (for 루프/if 문)를 약간 조정해야합니다. 이전에이 작업을 수행했으며 프로그래밍이 간단합니다.