如何自定义ActionBar?

时间:2017-05-16 03:03:10

标签: android

我正在使用android studio,带有设计库。问题是,库存actionbar没有edittext,所以我想添加自己的库存。我尝试了这段代码,但这是强制关闭。

 //support lib
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    //
   //custom ActionBar
    ActionBar actionBar = getActionBar();
    actionBar.setCustomView(R.layout.custom_actionbar);
    EditText search = (EditText) actionBar.getCustomView().findViewById(
            R.id.searchfield);
    search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                                      KeyEvent event) {
            Toast.makeText(MainActivity.this, "Search triggered",
                    Toast.LENGTH_LONG).show();
            return false;
        }
    });
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME);

自定义操作栏

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/searchfield"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textFilter" 
    android:text="Text">

</EditText>

日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{rsoft.clipsearch/rsoft.clipsearch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setCustomView(int)' on a null object reference

2 个答案:

答案 0 :(得分:0)

您可以将工具栏与此类

一起使用自定义用户界面
result = Porcelain.shell("C:\\PHP\\php.exe -f C:\\pap.php 613b8859")
IO.inspect result.out

答案 1 :(得分:0)

您可以自定义view,而不是将自定义ActionBar设置为Toolbar

1。在您的Toolbar布局activity中添加以下自定义XML布局:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp">

        <EditText
            android:id="@+id/searchfield"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textFilter"
            android:text="Text"
            android:maxLines="1" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

2。MainActivity中,将此Toolbar设置为支持ActionBar

// Custom toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

EditText search = (EditText) toolbar.findViewById(R.id.searchfield);
search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Toast.makeText(MainActivity.this, "Search triggered", Toast.LENGTH_LONG).show();

        return false;
    }
});

if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

3。theme下面使用MainActivity

//styles.xml
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

//AndroidManifest.xml
<activity
    android:name=".MainActivity"
    android:theme="@style/AppTheme.NoActionBar">

    ...........
    ...............
</activity>

<强>输出:

enter image description here

希望这会有所帮助〜