有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android设置自定义actionbar

在我的项目中,我想在一个特定的动作之后设置一个自定义的actionBar(带有自定义布局)。 我有一个简单的活动:

import 安卓.os.Bundle;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.view.Menu;
import 安卓.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));
}

}

其中“menu\u main\u activity2”是xml:

<menu xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
tools:context="zipangulu.myapplication.MainActivity2">
<item 安卓:id="@+id/action_settings"    安卓:title="@string/action_settings"
    安卓:orderInCategory="100" app:showAsAction="never" />
<item 安卓:id="@+id/information_item"
    安卓:title="Info"
    安卓:icon="@drawable/info"
    安卓:orderInCategory="100"
    app:showAsAction="always"/>
<item 安卓:id="@+id/search_item"
    安卓:title="Search"
    安卓:icon="@drawable/search_icon"
    安卓:orderInCategory="100"
    app:showAsAction="always"/>
</menu>

我会按主操作栏上的search_项,设置一个自定义操作栏,其布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent" 安卓:layout_height="wrap_content"
安卓:background="#4584d3">

<ImageButton
    安卓:layout_width="wrap_content"
    安卓:layout_height="match_parent"
    安卓:id="@+id/back_button"
    安卓:background="@null"
    安卓:src="@drawable/back_icon"/>

<AutoCompleteTextView
    安卓:layout_width="0dp"
    安卓:layout_height="match_parent"
    安卓:layout_weight="6"
    安卓:id="@+id/campo_ricerca"/>

<Spinner
    安卓:layout_width="0dp"
    安卓:layout_height="match_parent"
    安卓:layout_weight="2"
    安卓:spinnerMode="dropdown"
    安卓:id="@+id/spinner_anno"/>

<ImageButton
    安卓:layout_width="wrap_content"
    安卓:layout_height="match_parent"
    安卓:src="@drawable/search_icon"
    安卓:id="@+id/avvia_ricerca"/>

</LinearLayout>

但是在运行时,“ApriBararicerca”方法的主体中有一个NullPointerException。。为什么?我如何解决这个问题

编辑:正如建议的那样,我将“getActionBar()”替换为“getSupportActionBar()”,现在我没有任何异常,但什么也没有发生

EDIT2:我添加了getSupportActionBar()。setDisplayShowCustomEnabled(真);现在我的自定义actionbar可见,但不是我想要的,请看下图: http://bit.ly/1Dc2kGg 该栏可见,但已剪切,并且可以看到上一个操作栏中的项目


共 (1) 个答案

  1. # 1 楼答案

    您可以尝试使用android.support.v7.widget.ToolBar执行相同的操作

    您可以根据需要填充的布局:

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

    您必须修改值->;风格。xml:

    <resources>
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        </style>
    </resources>
    

    onCreate()方法中,您可以执行以下操作:

    Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
    if(tb != null){
        setSupportActionBar(tb);
    }
    

    希望对你有帮助