[英]Android setting custom actionbar
In my project i would to set a custom actionBar (with a custom layout) after a specific action. 在我的项目中,我将在特定操作之后设置自定义actionBar(具有自定义布局)。 I have this simple Activity:
我有这个简单的活动:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
@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_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//apriImpostazioni ();
return true;
case R.id.information_item:
//apriInformazioni ();
return true;
case R.id.search_item:
apriBarraRicerca ();
System.out.println ("IL BOTTONE RICERCA E' PREMUTO");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void apriBarraRicerca () {
getActionBar ().setCustomView(getLayoutInflater().inflate(R.layout.search_layout, null));
}
}
Where "menu_main_activity2" is this xml: 其中“ menu_main_activity2”是此xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="zipangulu.myapplication.MainActivity2">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/information_item"
android:title="Info"
android:icon="@drawable/info"
android:orderInCategory="100"
app:showAsAction="always"/>
<item android:id="@+id/search_item"
android:title="Search"
android:icon="@drawable/search_icon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
I would, pressing the search_item on the main actionbar, set a custom actionBar whith this layout: 我将按下主操作栏上的search_item,以这种布局设置自定义actionBar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#4584d3">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/back_button"
android:background="@null"
android:src="@drawable/back_icon"/>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="@+id/campo_ricerca"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown"
android:id="@+id/spinner_anno"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/search_icon"
android:id="@+id/avvia_ricerca"/>
</LinearLayout>
But at runtime I have a NullPointerException in the body of "apriBarraRicerca" method..why?How can I fix this problem? 但是在运行时,“ apriBarraRicerca”方法的主体中有一个NullPointerException。为什么?如何解决此问题?
EDIT: as suggested I replaced "getActionBar ()" with "getSupportActionBar ()", now I don't have any exception but nothing happens. 编辑:按照建议,我将“ getActionBar()”替换为“ getSupportActionBar()”,现在我没有任何异常,但是什么也没有发生。
EDIT2: I added getSupportActionBar().setDisplayShowCustomEnabled(true); EDIT2:我添加了getSupportActionBar()。setDisplayShowCustomEnabledEnabled(true); and now my custom actionbar is visible but not as i want, look at the following image: http://bit.ly/1Dc2kGg The bar is visible but cutted, and are visible also the items of the previous actionBar..
现在我的自定义操作栏是可见的,但不是我想要的,请看以下图像: http : //bit.ly/1Dc2kGg该栏是可见的,但已切掉,并且也可以看到上一个actionBar的项目。
You can try do the same thing with the android.support.v7.widget.ToolBar
您可以尝试使用
android.support.v7.widget.ToolBar
做同样的事情
Your Layout which you can populate as you desire: 您可以根据需要填充的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#688A08"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
You have to modify the values->styles.xml: 您必须修改values-> styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
And the in the onCreate()
method you do the following: 然后在
onCreate()
方法中执行以下操作:
Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
if(tb != null){
setSupportActionBar(tb);
}
Hope it helps!!! 希望能帮助到你!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.