【发布时间】:2015-12-15 09:17:01
【问题描述】:
我正在努力将Toolbar 放在屏幕底部。隐藏工具栏标题后,标题仍然占用空间。
我的问题是:如何删除空格?
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
在我的活动中onCreate():
toolbar = (Toolbar)findViewById(R.id.bar_bottom);
setSupportActionBar(toolbar);
getSupportActionBar().setSubtitle("");
getSupportActionBar().setDisplayShowTitleEnabled(false);
在同一活动的onCreateOptionsMenu 和onOptionsItemSelected:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_bar_bottom, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
在我的布局中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WelcomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/lbl_test"
android:layout_width="match_parent"
android:layout_height="20sp"
android:text=""
/>
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/bar_bottom"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
和菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context=".WelcomeActivity">
<item
android:id="@+id/btn_1"
android:orderInCategory="100"
android:title="Button 1"
app:showAsAction="always" />
<item
android:id="@+id/btn_2"
android:orderInCategory="200"
android:title="Button 2"
app:showAsAction="always" />
<item
android:id="@+id/btn_3"
android:orderInCategory="300"
android:title="Button 3"
app:showAsAction="always" />
<item
android:id="@+id/btn_4"
android:orderInCategory="400"
android:title="Button 4"
app:showAsAction="always" />
</menu>
【问题讨论】:
-
我没有隐藏工具栏,我只是通过
getSupportActionBar().setDisplayShowTitleEnabled(false);隐藏了工具栏标题 -
将重力设置为工具栏为 android:gravity="start"
-
菜单项总是从右边开始。
-
我知道,但我希望 4 个菜单项均等地占据全宽。我怎样才能做到这一点?
-
@Raptor 如果您以这种方式使用工具栏,您将无法随意对齐内容。相反,您可以将自定义布局添加到您的工具栏,然后自己管理所有项目点击,而无需将其设置为 ActionBar(这很糟糕,因为您必须编写所有侦听器)。看来你的目标,比如G+底栏,只有这样才能实现。