android中的自定义tabview

时间:2011-09-05 11:57:53

标签: android customization 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);

2 个答案:

答案 0 :(得分:1)

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

答案 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);
    }
}