2013-07-18 92 views
4

我开发在Android的聊天应用程序,我想添加动态的聊天窗口依赖于电流匹配用户喜欢附着在下面的截图:如何创建Android应用程序选项卡和动态添加标签(取决于匹配用户)

enter image description here

在屏幕截图的聊天窗口都在顶部,但我想Chat Tabs at bottom。现在我想开发onCreate method逻辑,这样

如果有三个匹配的用户,然后创建3个标签,

如果有四个匹配的用户,然后创建4个标签,同样..

我搜索了很多的聊天窗口,并找到了利用TabHost ..但创建的聊天窗口还发现,它已被弃用,不知道..另一种方法是设置聊天窗口中Action Bar ..Somewhere发现,使用ActionBarSherlock。我很困惑聊天标签,使用什么?

任何帮助将不胜感激。

回答

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

现在的Android的最新版本包含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了,但现在我已经迁移到新的Android SDK 18操作栏COMPAT。您的项目看起来像一个简单的实现操作栏选项卡,我看不出有任何理由你应该开始在这一点上使用ActionBarSherlock(或tabHost)。

操作栏COMPAT是非常相似的操作栏夏洛特和具有的优点是它是Android V4支持库的一个组成部分(compatable回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语句)以确定要添加多少个选项卡。我之前完成了这个任务,并且它是直接编程的。

相关问题