我在全屏模式下有活动。在活动中是一个带有4个标签的ActionBar。 我的问题是我想在活动的顶部“模拟”我自己的状态栏。但我无法在状态栏上方添加视图。
当我将自定义视图设置为ActionBar(并设置.setDisplayShowHomeEnabled(true))时,它基本上可以工作 - 自定义视图位于ActionBar-Tabs之上。但是在这个解决方案中,我无法控制自定义视图的高度 - 它始终静态地保持相同的高度。
您有什么想法来解决这个问题吗?
非常感谢和最诚挚的问候 托比
答案 0 :(得分:0)
如果你想要,你可以做到这一点,避免采取行动吧
不要使用操作栏,而是像操作栏一样创建线性或相对布局。
为布局指定与操作栏完全相同的固定高度。您可以从zip中的模板找到不同场景的操作栏的确切高度。 http://developer.android.com/downloads/design/Android_Design_Downloads_20130814.zip
然后使用带有片段的Tabhost来显示选项卡,因为tabhost可以放置在您可以随意使用它们的任何位置。
如果你想继续这种方式让我知道我会帮助你使用tabhost代码来显示标签。
这是带有片段
的TAbhost示例代码1.>将此添加到您的xml
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_weight="0"
android:background="@null"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
您还可以使用viewpager替换第二个framelayout以提供可转换标签,如果您打算这样做,则不要更改ID,只需将viewLayout标签替换为viewpager
2&GT;将此代码放在您的活动中
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);
mTabManager.addTab(
mTabHost.newTabSpec("fragment1").setIndicator("Fragment1"), FragmentOne.class,
null);
mTabManager.addTab(
mTabHost.newTabSpec("fragment2").setIndicator("Fragment2"),
FragmentTwo.class, null);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tab", mTabHost.getCurrentTabTag());
}
/**
* This is a helper class that implements a generic mechanism for
* associating fragments with the tabs in a tab host. It relies on a trick.
* Normally a tab host has a simple API for supplying a View or Intent that
* each tab will show. This is not sufficient for switching between
* fragments. So instead we make the content part of the tab host 0dp high
* (it is not shown) and the TabManager supplies its own dummy view to show
* as the tab content. It listens to changes in tabs, and takes care of
* switch to the correct fragment shown in a separate content area whenever
* the selected tab changes.
*/
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);
// 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.
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.getFragmentManager().executePendingTransactions();
}
}
这是一个正在运行的代码,您无需在addTab方法中更改除Fragments之外的任何内容,将片段放在那里。
对于每个片段,您都调用了addTab方法。