gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:04:07 25 4
gpt4 key购买 nike

我有一个包含五个选项卡的 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();
}
}
}
}

最佳答案

我从您的 AndroidManiest.xml 文件中假设您的每个选项卡都是 Activity 。如果是这样,那么看看Activity.setRequestedOrientation(int)

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

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

在所有其他 Tab Activity 中,您可以转 ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR上。

关于Android:在单个选项卡上锁定屏幕方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12012354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com