Android:在单个选项卡上锁定屏幕方向

时间:2012-08-17 19:54:56

标签: android android-manifest android-tabhost screen-orientation

我有一个带有五个标签的TabHost,我们将其称为标签1-5。

我希望Tab 3保持画像 - 我不希望它永远属于风景画。

我将如何实现这一目标?

这是我当前的代码(Main.class):

public class Main extends FragmentActivity {
    TabHost mTabHost;
    TabManager mTabManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

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

        mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

        addTab(mTabManager, mTabHost, new String("Tab 1"), new String("tab1"),
            Tab1.class);
        addTab(mTabManager, mTabHost, new String("Tab 2"), new String("tab2"),
            Tab2.class);
        addTab(mTabManager, mTabHost, new String("Tab 3"), new String("tab3"),
            Tab3.class);
        addTab(mTabManager, mTabHost, new String("Tab 4"), new String("tab4"),
            Tab4.class);
        addTab(mTabManager, mTabHost, new String("Contact"), new String("contact"),
            Tab5.class);

        mTabHost.getTabWidget().getChildTabViewAt(0).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(1).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(2).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(3).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(4).setFocusable(false);
        mTabHost.setSelected(false);
        mTabHost.getTabWidget().setSelected(false);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    private static View prepareTabView(Context context, String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null);
        ((TextView) view.findViewById(R.id.tabsText)).setText(text);
        return view;
    }

    public static void addTab(TabManager manager, TabHost host, String title, String tag, Class<?> cl) {
        TabHost.TabSpec spec = host.newTabSpec(tag);
        View view = prepareTabView(host.getContext(), title);
        spec.setIndicator(view);

        manager.addTab(spec, cl, null);
    }

    public static class TabManager implements TabHost.OnTabChangeListener {
        private final FragmentActivity mActivity;
        private final TabHost mTabHost;
        private final int mContainerId;
        private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
        TabInfo mLastTab;

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
            mActivity = activity;
            mTabHost = tabHost;
            mContainerId = containerId;
            mTabHost.setOnTabChangedListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mActivity));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);

            info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
            if (info.fragment != null && !info.fragment.isDetached()) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                ft.detach(info.fragment);
                ft.commit();
            }

            mTabs.put(tag, info);
            mTabHost.addTab(tabSpec);
        }

        @Override
        public void onTabChanged(String tabId) {
            TabInfo newTab = mTabs.get(tabId);
            if (mLastTab != newTab) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                if (mLastTab != null) {
                    if (mLastTab.fragment != null) {
                        ft.detach(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                        ft.add(mContainerId, newTab.fragment, newTab.tag);
                    } else {
                        ft.attach(newTab.fragment);
                    }
                }

                mLastTab = newTab;
                ft.commit();
                mActivity.getSupportFragmentManager().executePendingTransactions();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我假设您的AndroidManiest.xml文件中的每个标签都是活动。 如果是,请查看Activity.setRequestedOrientation(int)

在Tab3 Activity类中,您可以使用以下代码将活动锁定为纵向模式:

public class Tab3 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

在所有其他标签活动中,您可以启用ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR

答案 1 :(得分:0)

最简单的方法是转到manifest.XML

在您想要的任何活动中,以粗体

添加此行
        android:screenOrientation="portrait"

在你的情况下:

 <activity
            android:name=".Main"
            android:screenOrientation="portrait" />