Как добавить кнопки, такие как обновление и поиск в панели инструментов в Android?



enter image description here



Я сделал панель инструментов, но когда я добавляю пункты меню в меню.XML, он всегда показывает как переполнение.
Как добавить его отдельно?
Кроме того, название показано в середине (вертикально), как я могу показать его в верхней части?



.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="com.example.toolbar.MainActivity" >

<item android:id="@+id/action_search"
android:icon="@drawable/ic_launcher"
android:title="@string/action_search"
android:showAsAction="ifRoom"
android:orderInCategory="1"
android:menuCategory="secondary"/>

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>

</menu>


MainActivity.java



package com.example.toolbar;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
// toolbar.setNavigationIcon(R.drawable.back);
toolbar.setLogo(R.drawable.ic_launcher);
//toolbar.setTitle("Title");
// toolbar.setSubtitle("Subtitle");





}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


activity_main.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.toolbar.MainActivity" >

<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="fill_parent"
android:layout_height="128dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ActionBarThemeOverlay">
</android.support.v7.widget.Toolbar>



</RelativeLayout>
455   4  

4 ответов:

ОК, я получил значки, потому что я написал в меню.xml android:showAsAction="ifRoom" вместо app:showAsAction="ifRoom" так как я использую библиотеку v7.

однако название идет в центре расширенной панели инструментов. Как заставить его появиться наверху?

попробуйте сделать это:

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);

и если вы сделали вашу пользовательскую панель инструментов (который я предполагаю, что вы сделали), то вы можете использовать самый простой способ сделать это:

toolbarTitle = (TextView)findViewById(R.id.toolbar_title);
toolbarSubTitle = (TextView)findViewById(R.id.toolbar_subtitle);
toolbarTitle.setText("Title");
toolbarSubTitle.setText("Subtitle");

то же самое относится к любым другим представлениям, которые вы помещаете в Панель инструментов. Надеюсь, это поможет.

добавьте эту строку вверху:

"xmlns:app="http://schemas.android.com/apk/res-auto"

и затем использовать:

app:showasaction="ifroom"

для управления расположением заголовка вы можете установить пользовательский шрифт, как описано здесь (по twaddington):ссылке

затем, чтобы переместить положение текста, в updateMeasureState() добавить p.baselineShift += (int) (p.ascent() * R); Аналогично в updateDrawState() добавить tp.baselineShift += (int) (tp.ascent() * R); Где R-удвоение между -1 и 1.

Comments

    Ничего не найдено.