2015-10-19 217 views
0

我正在使用新的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

回答

0

我找到了解決你的問題(也許爲時已晚...),但下面是我用來做它的代碼:

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