【问题标题】:toolbar not showing in the bottom of the screen工具栏未显示在屏幕底部
【发布时间】:2015-10-09 05:21:38
【问题描述】:

为什么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>

我只在顶部获得工具栏

【问题讨论】:

标签: android


【解决方案1】:

试试这个方法

<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>

【讨论】:

  • @Moudiz 编码快乐:)
【解决方案2】:

如果您已经定义了android:layout_alignParentBottom="true",则无需添加android:layout_gravity="bottom"

更多问题可以访问这里。希望对你有帮助。

How to show action items at the bottom using Toolbar

【讨论】:

  • 即使删除了 android:layout_gravity="bottom" 它仍然没有显示,正如我在从您提到的链接中获取代码之前评论的那样
  • @Lakhan (Y) 继续前进
  • 我还没有测试它,但我想用适配器图像制作一个网格视图。我面临一些问题。今晚发个问题希望你能解决。有美好的一天@Lakhan intellij
  • @Moudiz 我们会尽力做到最好的水平-问候 Amiya 和 Lakhan
  • @IntelliJAmiya Lakkahn 我不知道你是否知道 Okhttp,但你能在这段代码中帮助我吗? stackoverflow.com/questions/33089581/…
【解决方案3】:

试试下面的,这对我有用

<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>

【讨论】:

    【解决方案4】:

    可能问题出在 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);
    }
    

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2016-10-14
    • 2021-12-28
    • 2022-11-09
    • 1970-01-01
    相关资源
    最近更新 更多