如何使用手机/平板电脑正确缩放Android标签

时间:2014-08-16 21:59:01

标签: android android-actionbar actionbarsherlock android-tabs

我的平板电脑布局中的sherlock-tabs存在缩放问题,如下图所示:

enter image description here

出于某种原因,平板电脑布局无法像在手机布局中那样正确缩放。平板电脑布局中标签的宽度不够宽。如果无法使平板电脑布局中的标签宽度更大,我想这样做。如果没有,我至少希望将它们移到左边。

可以在白色搜索选项卡(选定选项卡)的左侧看到此问题。我想left_align标签,以便它不会留下空白空间。

如何在不使手机布局看起来像废话的情况下完成此操作?我的代码应该怎么做?

Tabs.java (在java中实现标签)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_host);

        settings = Injecter.settings(this);
        setOnCreatePreferences();

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        actionBar.setDisplayShowCustomEnabled(false);

        actionBar.setTitle(R.string.LabelTitleAbout);

        actionBar.addTab(actionBar.newTab().
                 //setText(R.string.LabelSearchTabTitle).
            setIcon(R.drawable.ic_tab_search).
                //setTabListener(this));
                setTabListener(new TabListener<LandingSearch>(this, getString(R.string.LabelSearchTabTitle), LandingSearch.class, null)));

*code continues with the rest of the tabs...*

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private final FragmentActivity mActivity;
        private final String mTag;
        private final Class<T> mClass;
        private final Bundle mArgs;
        private Fragment mFragment;

        public TabListener(FragmentActivity activity, String tag, Class<T> clz, Bundle args) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
            mArgs = args;
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

            // 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.
            mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached()) {
                ft.detach(mFragment);
            }
            ft.commit();
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction transaction) {
            if (mFragment == null) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 
                mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            //  ft.add(android.R.id.content, mFragment, mTag);
                ft.replace(R.id.tabs_navigation_content, mFragment, mTag);
                ft.commit();
            } else {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 
                ft.attach(mFragment);
                ft.commit();
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
            if (mFragment != null) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 
                ft.detach(mFragment);
                ft.commitAllowingStateLoss();
            }
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {

        }
    }
}

tab_host.xml (从tabs.java代码中为标签设置contentView。不确定我是否遗漏了任何属性,这会使平板电脑的缩放变得奇怪?)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:id="@+id/tabs_navigation_content"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </LinearLayout>
</LinearLayout>

tab_bar_background.xml (从themes.xml设置tabBar背景)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tabbar_brown"/>
  <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_bar_background_selected"/>
  <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tabbar_grey"/>
  <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_bar_background_selected_pressed"/>
</selector>

tab_bar_background_selected.xml (定义所选标签应具有白色背景)

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-5dp" android:left="-5dp" android:right="-5dp" android:drawable="@drawable/tabbar_white" >
        <shape android:shape="rectangle">
            <stroke android:color="@color/black" android:width="-5dp"/>

        </shape>
    </item>
</layer-list>

themes.xml (actionBar和tabBar的常规设置)

<style name="Theme.ML" parent="@style/Theme.Sherlock">
         <item name="android:windowBackground">@drawable/bg_math</item>
         <item name="android:actionBarTabStyle">@style/Theme.ML.Tabs</item>
         <item name="actionBarTabStyle">@style/Theme.ML.Tabs</item>

         <item name="actionBarTabBarStyle">@style/Theme.ML.Tabs</item>
         <item name="android:actionBarTabBarStyle">@style/Theme.ML.Tabs</item>

         <item name="actionBarStyle">@style/Theme.ML.Tabs</item>
         <item name="android:actionBarStyle">@style/Theme.ML.Tabs</item>  

    </style>

    <style name="Theme.ML.ActionBar" parent="@style/Theme.Sherlock">
         <item name="android:windowBackground">@drawable/bg_math</item>
         <item name="android:actionBarTabStyle">@style/Theme.ML.Tabs</item>
         <item name="actionBarTabStyle">@style/Theme.ML.Tabs</item>

         <item name="actionBarTabBarStyle">@style/Theme.ML.Tabs</item>
         <item name="android:actionBarTabBarStyle">@style/Theme.ML.Tabs</item>

         <item name="actionBarStyle">@style/Theme.ML.Tabs</item>
         <item name="android:actionBarStyle">@style/Theme.ML.Tabs</item>  

         <item name="actionBarTabTextStyle">@style/Theme.ML.Text</item>
         <item name="android:actionBarTabTextStyle">@style/Theme.ML.Text</item>
    </style>

    <style name="Theme.ML.Tabs" parent="@style/Widget.Sherlock.ActionBar.TabBar">

        <item name="windowActionBarOverlay">true</item>  
        <item name="windowActionModeOverlay">true</item>  

        <item name="background">@drawable/tab_bar_background</item> 
        <item name="android:background">@drawable/tab_bar_background</item>
    </style>

此致

0 个答案:

没有答案