2

我已经搞砸了两天了,但似乎仍然无法弄清楚。

我终于在我的所有活动中看到了 tabhost,但是为了做到这一点,我必须创建一个片段活动。这弄乱了我的标签按钮的布局。当我使用选项卡活动时,它工作得很好,但现在,它看起来像这样:

在此处输入图像描述

我希望蓝色按钮占据整个栏。没有像现在这样的背景空间。不知道是布局问题还是缩放问题?这是我创建标签的代码,也是我调整标签大小的地方,以便中间的标签是外部标签的两倍:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        initialiseTabHost(savedInstanceState);


        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }

        // the following code is used to decrease the size of the two end tabs
                Display display = getWindowManager().getDefaultDisplay();
                int width = display.getWidth();

                TabHost mth = (TabHost)findViewById(android.R.id.tabhost);

                mth.getTabWidget()
                        .getChildAt(0)
                        .setLayoutParams(
                                new LinearLayout.LayoutParams(width / 4,
                                        (int) (width / 4 * .625)));

                mth.getTabWidget()
                        .getChildAt(1)
                        .setLayoutParams(
                                new LinearLayout.LayoutParams(width / 2,
                                        (int) (width / 4 * .625)));
                mth.getTabWidget()
                        .getChildAt(2)
                        .setLayoutParams(
                                new LinearLayout.LayoutParams(width / 4,
                                        (int) (width / 4 * .625)));


    }

    /** (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
     */
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }

    /**
     * Initialise the Tab Host
     */



    private void initialiseTabHost(Bundle args) {


        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;

        Resources res = getResources();

        fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("", res.getDrawable(R.drawable.tab_home)), ( tabInfo = new TabInfo("Tab1",ArrowsActivity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("", res.getDrawable(R.drawable.tab_search)), ( tabInfo = new TabInfo("Tab2", OptionsActivity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("", res.getDrawable(R.drawable.twitter)), ( tabInfo = new TabInfo("Tab3", ArrowsActivity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        // Default to first tab
        this.onTabChanged("Tab1");
        //
        mTabHost.setOnTabChangedListener(this);


        }

    /*
     * @param activity
     * @param tabHost
     * @param tabSpec
     * @param clss
     * @param args
     * */

    private static void addTab(fragactivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }


        tabHost.addTab(tabSpec);
}

这是旧代码(当按钮大小正确时。)看起来视图正在通过另一个 .xml 文件传递​​,该文件指定图标应与父级匹配。我尝试在新代码中复制它,但我认为这对我来说有点太复杂了。这是与 .xml 一起使用的代码:

private void setTabs() {

        // add tabs
        addTab("Home", R.drawable.tab_home, ArrowsActivity.class);
        addTab("Attendance", R.drawable.tab_search, OptionsActivity.class);
        addTab("@PSUStrength", R.drawable.twitter, OptionsActivity.class);
        // addTab("Contact Us", R.drawable.contact, OptionsActivity.class);
    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, getTabWidget(), false);
        // TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        // title.setText(labelId);

        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);

        // registerForContextMenu(icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);

    }

tab_indicator.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="55dip"    
    android:layout_weight="1"
    android:orientation="vertical">

    <ImageView android:id="@+id/icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:src="@drawable/icon"

    /> 

</RelativeLayout>

我真的很感激这方面的一些帮助。谢谢。

4

1 回答 1

1

您好,我在文本选项卡上遇到了同样的问题,而不是图像,但我认为该解决方案适用于这两种情况。

在 TabWidget 内,您之前输入的每个指标都有一个 LinearLayout(在下面的代码中查看 childAt)。此 LinearLayout 充当指标文本/图像的容器。默认情况下,此 LinearLayout 有一些填充。在我的情况下,这是 24。我只是降低了它。

将 TabSpec 实例添加到 TabHost 后,输入以下代码

    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
    View childAt = mTabHost.getTabWidget().getChildAt(i);
    childAt.setPadding(childAt.getPaddingLeft()/2,
        childAt.getPaddingTop(),
        childAt.getPaddingRight()/2,
        childAt.getPaddingBottom());
}

保持这种动态是一个很好的建议,这就是为什么我给出了以前的一半。如果您设置一个固定数字,您可能会遇到其他(较小)分辨率的问题。

如果您的选项卡上有文本,您可能还想对其进行操作。像这样做:

        TextView tv = (TextView) childAt.findViewById(android.R.id.title);
        //do whatever change in tv you might need, eg.
        tv.setSingleLine();
于 2014-06-11T14:29:57.810 回答