当前位置: 代码迷 >> Android >> 如何更改Android工具栏视图?
  详细解决方案

如何更改Android工具栏视图?

热度:33   发布时间:2023-08-04 09:46:23.0

我正在使用新的android工具栏。

/* Jump into, after the user clicks on a listview item */
private void toogleToolbar() {
    if (isStandardToolbar)
        customToolbar();
    else
        originalToolbar();

    isStandardToolbar = !isStandardToolbar;
}

/* Called inside onCreate and if nothing was clicked */
private void originalToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}


/* Called after an click event */
private void customToolbar() {
    LayoutInflater inflater = this.getLayoutInflater();
    Toolbar toolbar = (ToolBar) inflater.inflate(R.layout.newtoolbar, null);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}

到目前为止,它仍然有效,但是现在我想在单击列表元素后更改工具栏视图。 那是我的问题,因为最后一个代码片段不会产生异常或其他问题,所以应该没问题。 但是我只看到“旧的”工具栏。 我试图将可见性设置为“旧”或“不可见”,但它没有任何作用。

在我的activity_main.xml中,包含R.id.toolbar,但是我认为,第二个代码必须覆盖旧代码!

编辑:AFAIK,新的工具栏应替换旧的actionBar。 工具栏用于放置导航或其他特定内容。 以我为例,我想创建一个小的操作菜单,用户可以在其中编辑或删除列表项。

我找到了您问题的解决方案(也许为时已晚...),但是下面是我用来编写的代码:

MainActivity.java

package fr.zwedge.sot;

import android.app.*;
import android.os.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;

public class MainActivity extends AppCompatActivity {

boolean isStandardToolbar = true;
android.support.v7.widget.Toolbar toolbar = null, newToolbar = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initToolbars();
    ((Button)toolbar.findViewById(R.id.tbt)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            toogleToolbar();
        }
    });
    ((Button)newToolbar.findViewById(R.id.tbt2)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            toogleToolbar();
        }
    });
}

/* Jump into, after the user clicks on a listview item */
private void toogleToolbar() {
    if (isStandardToolbar)
        customToolbar();
    else
        originalToolbar();
    isStandardToolbar = !isStandardToolbar;
}

private void initToolbars() {
    if (toolbar == null)
        toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar);
    if (newToolbar == null)
        newToolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.newtoolbar);
}

/* Called inside onCreate and if nothing was clicked */
private void originalToolbar() {
    toolbar.setVisibility(View.VISIBLE);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}


/* Called after an click event */
private void customToolbar() {
    toolbar.setVisibility(View.GONE);
    setSupportActionBar(newToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
}
}

这是main.xml中的两个工具栏:

<LinearLayout 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="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/holo_green_dark"
    android:background="#FF008F12">
    <Button
        android:text="Bla bla bla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/tbt" />
</android.support.v7.widget.Toolbar>

<android.support.v7.widget.Toolbar
    android:id="@+id/newtoolbar"
    android:minHeight="?attr/actionBarSize"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/holo_green_dark"
    android:background="#FF8F0800">
    <Button
        android:text="Change once again"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tbt2" />
</android.support.v7.widget.Toolbar>

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/text_view" />

希望对您有帮助,Darkball60

  相关解决方案