在我的应用程序中,我在底部使用了操作栏选项卡。我搜索了很多教程,但我不了解这些解决方案。大多数答案都表示使用TabActivity,但已弃用。那么,任何人都可以告诉我如何处理这个问题?如何在底部设置标签并隐藏顶部操作栏?
3
A
回答
0
第1步 - 为您的工具栏创建自定义布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
第2步 - 将此布局包含到您的活动布局中。给它的底部位置。
<include
android:id="@+id/toolbar_main"
layout="@layout/main_toolbar" />
第3步 - 调用工具栏进入您的活动和完成。它将表现为任何工具栏,但位于底部。
3
这是一个例子
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.action_settings:
// TODO
break;
// TODO: Other cases
}
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbarBottom.inflateMenu(R.menu.menu_main);
这个XML
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_bottom"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_alignParentBottom="true"
android:minHeight="?attr/actionBarSize" />
14
现在实行的标签,最好的办法是使用TabLayout从设计库。
这里是一个TabLayout在屏幕底部对齐的例子。
首先建立在的build.gradle文件中的依赖关系:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
下面是主要活动的布局XML:
<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
app:tabMode="fixed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#ffffff"
app:tabIndicatorColor="#ff00ff"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
下面是活动代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three", };
Context context;
public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new BlankFragment();
case 1:
return new BlankFragment();
case 2:
return new MapFragment();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
}
}
custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/custom_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:textSize="16dip"
android:textColor="#ffffff"
android:singleLine="true"
/>
</LinearLayout>
应用主题styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#3F51B5</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#303F9F</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FF4081</item>
</style>
结果:
如果你想隐藏的动作条,只需从布局XML中删除工具栏,并从“活动”中删除工具栏代码,并确保在AppTheme中使用Theme.AppCompat.Light.NoActionBar
。这些变化的结果将使它看起来像这样:
相关问题
- 1. 顶部和底部的标签栏
- 2. 如何隐藏Android中的底部操作栏?
- 3. 按需隐藏底部栏
- 4. Android - 顶部的动作栏和页面底部的标签
- 5. 如何在两个操作栏中分割图标 - 顶部和底部?
- 6. 如何设置UIToolBarPosition顶部或底部
- 7. 在iPhone中打开Admob广告时隐藏底部标签栏
- 8. 如何在android的底部设置标签栏?
- 9. 使用CoordinatorLayout时隐藏顶部和底部工具栏
- 10. 如何将动态创建的操作栏设置为底部?
- 11. 即使鼠标悬停在“youtube”顶部,如何隐藏栏杆?
- 12. 隐藏操作栏同时显示如何隐藏顶部操作栏,但使用ActionBarSherlock显示拆分操作栏拆分操作栏
- 13. 在Android中隐藏底部选项栏
- 14. Android:是否可以隐藏操作栏的顶部,但保持底部栏的溢出按钮
- 15. 如何知道Android操作栏的动作图标是否位于顶部栏或底部栏(分割)?
- 16. 隐藏底部导航栏 - 在底部显示白色条
- 17. 底部带有mapview的标签顶部
- 18. matplotlib colorbar交替顶部底部标签
- 19. 如何隐藏滚动顶部或底部的tabhost
- 20. 记事本++标签栏的底部而不是顶部?
- 21. 底部而不是顶部的操作栏
- 22. 隐藏并显示标签在操作栏中
- 23. 如何保持顶部的操作栏
- 24. 如何以编程方式设置“在推入时隐藏底部栏”?
- 25. 导航栏隐藏NestedScrollView的底部[Android]
- 26. QLPreviewController隐藏底部工具栏
- 27. Android标签组活动显示和隐藏底部标签
- 28. Android中的顶部标签,底部标签和动作条标签
- 29. 如何在页面顶部滚动并隐藏边栏时显示边栏?
- 30. 底部tabBar隐藏
谢谢..这是我的工作......谢谢verymuch –
良好的工作@Daniel – saintjab
@丹尼尔 - 纽金特我怎样才能设置粉红在tablayout顶部的指标?由于tablayout位于底部,所以粉色指标在顶部时更有意义。 – Felix