Android:标签不用Holo主题填充父级?

时间:2012-06-06 21:53:20

标签: android

以下代码创建一个包含4个标签的视图。使用默认主题(没有在androidmanifest.xml中定义主题),我得到第一个图像,其中选项卡平均增长以填充可用空间。如果我将主题设置为android:theme="@android:style/Theme.Holo",我会得到第二个图像,其中tabwidget(带有绿色背景)仍然填充父级的宽度,但是标签不会增长以填充tabwidget。

关于如何在使用Theme.Holo时获得第一个结果的任何想法?

enter image description here

enter image description here

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

        setContentView(new MyTabHost(this));
    }

    private class MyTabHost extends TabHost
    {
        public MyTabHost(Context context) 
        {
            super(context, null);

            setId(android.R.id.tabhost);

            TabWidget widget = new TabWidget(getContext());
            widget.setId(android.R.id.tabs);
            widget.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            widget.setBackgroundColor(Color.GREEN);

            FrameLayout layout = new FrameLayout(getContext());
            layout.setId(android.R.id.tabcontent);
            layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

            LinearLayout linearLayout = new LinearLayout(getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setLayoutParams(new TabHost.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            linearLayout.addView(widget);
            linearLayout.addView(layout);

            addView(linearLayout);

            setup();

            TabSpec spec = newTabSpec("tab1");
            spec.setIndicator("", getContext().getResources().getDrawable(R.drawable.ic_launcher));
            spec.setContent(new TabContentFactory() 
            {
                @Override
                public View createTabContent(String tag) 
                {
                    return new View(getContext());
                }
            });
            addTab(spec);

            spec = newTabSpec("tab2");
            spec.setIndicator("", getContext().getResources().getDrawable(R.drawable.ic_launcher));
            spec.setContent(new TabContentFactory() 
            {
                @Override
                public View createTabContent(String tag) 
                {
                    return new View(getContext());
                }
            });
            addTab(spec);

            spec = newTabSpec("tab3");
            spec.setIndicator("", getContext().getResources().getDrawable(R.drawable.ic_launcher));
            spec.setContent(new TabContentFactory() 
            {
                @Override
                public View createTabContent(String tag) 
                {
                    return new View(getContext());
                }
            });
            addTab(spec);

            spec = newTabSpec("tab5");
            spec.setIndicator("", getContext().getResources().getDrawable(R.drawable.ic_launcher));
            spec.setContent(new TabContentFactory() 
            {
                @Override
                public View createTabContent(String tag) 
                {
                    return new View(getContext());
                }
            });
            addTab(spec);
        }

    }
}

编辑:

如果我在MyTabHost的构造函数末尾添加以下代码,我会得到下面的图像。但是,图标仍未在选项卡中居中,并且它似乎不是特别安全的代码段。

有什么比这更好的选择吗?或者对这是否合理的选择提出意见?

enter image description here

        post(new Runnable() 
        {

            @Override
            public void run() 
            {
                for(int i = 0; i < widget.getChildCount(); i++)
                {
                    widget.getChildAt(i).getLayoutParams().width = getWidth()/widget.getChildCount();
                }
            }
        });
    }

0 个答案:

没有答案