Java Android:主机图标不能占据全部空间?

Java Android:主机图标不能占据全部空间?,java,android,mobile,icons,android-tabhost,Java,Android,Mobile,Icons,Android Tabhost,这件事我已经搞了两天了,似乎还没弄明白 我终于让tabhost在我的所有活动中都可见了,但是为了做到这一点,我必须创建一个片段活动。这打乱了我的标签按钮的布局。当我使用tab activity时,它工作得非常好,但现在,它看起来是这样的: TextView tv = (TextView) childAt.findViewById(android.R.id.title); //do whatever change in tv you might need, eg.

这件事我已经搞了两天了,似乎还没弄明白

我终于让tabhost在我的所有活动中都可见了,但是为了做到这一点,我必须创建一个片段活动。这打乱了我的标签按钮的布局。当我使用tab activity时,它工作得非常好,但现在,它看起来是这样的:

        TextView tv = (TextView) childAt.findViewById(android.R.id.title);
        //do whatever change in tv you might need, eg.
        tv.setSingleLine();

我想让蓝色的按钮占据整个酒吧。没有像现在这样的背景空间。我不知道这是布局问题还是缩放问题?以下是我创建选项卡的代码,以及调整选项卡大小的代码,以便中间选项卡的长度是外部选项卡的两倍:

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);

    }
private void setTabs(){
//添加选项卡
addTab(“Home”,R.drawable.tab_Home,arrowsacActivity.class);
addTab(“考勤”,R.drawable.tab_搜索,选项Activity.class);
addTab(“@psustence”,R.drawable.twitter,optionActivity.class);
//addTab(“联系我们”,R.drawable.Contact,options activity.class);
}
私有void addTab(字符串labelId,int-drawableId,c类){
TabHost TabHost=getTabHost();
意向=新意向(本,c);
TabHost.TabSpec spec=TabHost.newTabSpec(“tab”+labelId);
视图选项卡indicator=layoutinger.from(this).充气(
R.layout.tab_指示器,getTabWidget(),false);
//TextView title=(TextView)tabIndicator.findViewById(R.id.title);
//标题:setText(labelId);
ImageView图标=(ImageView)选项卡indicator.findViewById(R.id.icon);
//registerForContextMenu(图标);
icon.setImageResource(drawableId);
规格设置指示器(tabIndicator);
规格设置内容(意图);
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>


我真的很感激你能帮我。谢谢。

您好,我对文本选项卡有相同的问题,而不是图像,但我认为解决方案适用于这两种情况

在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());
}

这个问题解决了吗?事实上,我面临着一些类似的问题。