2011-09-05 96 views
1

我想设计一个页面,我只想显示一个选项卡。但是,当我只添加一个选项卡,它需要全部可用的宽度,但我希望该选项卡只占用较小的空间,剩余空间留空。没有其他标签我想添加。一次只显示一个选项卡。代码如下android中的自定义tabview

public class TabViewActivity extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, FirstTab.class); 
     // Initialize a TabSpec for each tab and add it to the TabHost 

     spec = tabHost.newTabSpec("firstTab").setIndicator("First Tab") 
         .setContent(intent); 


     tabHost.addTab(spec); 
     tabHost.setCurrentTab(1); 

回答

0

在XML隐藏通过设置知名度水涨船高的标签控件。而是使用一个按钮。而在按钮处理程序,你可以使用如下代码

public void tabHandler(View target){ 
    artistButton.setSelected(false); 
    albumButton.setSelected(false); 
    if(target.getId() == R.id.artist_id){ 
     tabHost.setCurrentTab(0); 
     artistButton.setSelected(true); 
    } else if(target.getId() == R.id.album_id){ 
     tabHost.setCurrentTab(1); 
     albumButton.setSelected(true); 
    } 
} 
1

使用tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30;还设置了layout_width="wrap_content"

+0

感谢我尝试这样做,这是工作 – Vikas