工具栏未显示在屏幕底部

时间:2015-10-09 05:21:39

标签: android

为什么toolbar没有显示在底部?

private void initToolbars() {

    Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
    setSupportActionBar(toolbarTop);

    Toolbar toolbarBottom = (Toolbar) findViewById(R.id.drawer_layout);
    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;
        }
    });
    toolbarBottom.inflateMenu(R.menu.menu_main);

}

这个xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/drawer_layout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:minHeight="?attr/actionBarSize" />



</RelativeLayout>

我只在顶部获得工具栏

4 个答案:

答案 0 :(得分:2)

当您已定义android:layout_alignParentBottom="true"后,无需添加android:layout_gravity="bottom"

如果您有更多问题,请访问此处。我希望它能为您提供帮助。

  

How to show action items at the bottom using Toolbar

答案 1 :(得分:2)

试试这种方式

<RelativeLayout ...>
    <!-- top toolbar -->
    <Toolbar
        android:id="@+id/top_toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
    >

        ...

    </Toolbar>

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top_toolbar"
        android:src="@drawable/week1"
    />

    <!-- bottom toolbar -->
    <Toolbar
        android:id="@+id/bottom_toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:layout_alignParentBottom="true">

    </Toolbar>

    <!-- scrollable pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/image"
        android:layout_above="@id/bottom_toolbar">

        ...

    </ScrollView>
</RelativeLayout>

答案 2 :(得分:2)

尝试下面的内容,这对我有用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<!--Begin Top Bar-->
<android.support.v7.widget.Toolbar
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary" />
</LinearLayout>

<!--Bottom bar-->
<android.support.v7.widget.Toolbar
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:alignParentBottom="true"/></LinearLayout>

答案 3 :(得分:2)

可能是问题是用tw工具栏。

尝试使用以下代码:

<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"
tools:context=".MainActivity">


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentBottom="true"
    android:background="?attr/colorPrimary" />

您的MainActivity.java

public class MainActivityextends AppCompatActivity {

private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

@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_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

希望它对你有所帮助。