如何更改Android工具栏视图?

问题描述:

我正在使用新的android工具栏。如何更改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); 
} 

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

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

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

+0

其他添加一些代码。无论如何,我认为你没有正确使用对工具栏的调用。事实上,我敢打赌。尝试插入整个工具栏,并使用object.visibility(View.GONE)和View.VISIBLE进行播放。 – Maga

+0

我认为工具栏应该是活动布局的一部分。在其他情况下,它不会出现。您是否想根据某些条件对工具栏进行特定的布局? – Abdullah

+0

是的,我想在某些条件下进行其他布局。我做了我的第一篇文章。 – user3417078

我找到了解决你的问题(也许为时已晚...),但下面是我用来做它的代码:

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

希望它可以帮助ÿ ou,Darkball60