[英]How to add menu and settings option to the customized title bar of android?
My requirement is to increase the height of the title bar, however I am not sure if that is feasible. 我的要求是增加标题栏的高度,但我不确定这是否可行。 If it is how can I increase its height so I would have more space.
如果是我怎么能增加它的高度所以我会有更多的空间。 If it is not, I would need to add a menu option (Hamburger Style) to my customized title bar.
如果不是,我需要在我的自定义标题栏中添加一个菜单选项(汉堡风格)。
custom_title_bar.xml custom_title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#77b48e"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="29dp"
android:layout_height="28dp"
android:maxWidth="1dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/title_bar_viewname"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_weight="0.07"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
MainActivity.java MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
}
Change your MainActivity.java like this: 像这样更改您的MainActivity.java :
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.activity_main); actionBar = getActionBar(); ColorDrawable colorDrawable = new ColorDrawable( Color.parseColor("#373836")); actionBar.setBackgroundDrawable(colorDrawable); }
For an Eg: now your action bar will be shown like this: 对于例如:现在您的操作栏将显示如下:
Then if you need to add the setting menu
items in the action bar. 然后,如果您需要在操作栏中添加
setting menu
项。
Add this below code in MainActivity,java : 在MainActivity,java中添加以下代码:
@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) { switch (item.getItemId()) { case R.id.search: //your code here return true; default: return super.onOptionsItemSelected(item); } }
Then in res/menu/main.xml: 然后在res / menu / main.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" > <item android:id="@+id/search" android:icon="@drawable/ic_action_settings" android:showAsAction="always" android:title="text"/> </menu>
Now you can see the settings icon in the action bar 现在,您可以在操作栏中看到设置图标
Finally if you have to increase the Action bar Height : 最后,如果你必须增加动作栏高度 :
In manifest : 在清单中 :
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > --->set a theme ... </application>
Then in Styles.xml : 然后在Styles.xml中 :
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"></style> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:actionBarSize">80dip</item> </style> </resources>
Now Action bar size is increased upto the size we given in styles. 现在,操作栏大小增加到我们在样式中给出的大小。
With the release of android L, a new view has been released: Toolbar. 随着Android L的发布,一个新的视图已经发布:工具栏。 Toolbar gives you much more flexibility to style the action Bar.
工具栏为您设置动作栏的样式提供了更大的灵活性。 It is also available for lower android versions via the appCompat library.
它也可以通过appCompat库用于较低的Android版本。
In this release, Android introduces a new Toolbar widget.
在此版本中,Android引入了一个新的工具栏小部件。 This is a generalization of the Action Bar pattern that gives you much more control and flexibility.
这是Action Bar模式的概括,为您提供更多控制和灵活性。 Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate it, and react to scroll events.
工具栏是层次结构中的视图,与其他视图一样,可以更轻松地与其余视图交错,为其设置动画并对滚动事件做出反应。 You can also set it as your Activity's action bar, meaning that your standard options menu actions will be display within it.
您也可以将其设置为Activity的操作栏,这意味着您的标准选项菜单操作将显示在其中。
You can change the height of your action bar by using Toolbar and just setting the layout_height attribute to the height you want. 您可以使用工具栏更改操作栏的高度,只需将layout_height属性设置为所需的高度即可。
More info on how to use it: 有关如何使用它的更多信息:
http://android-developers.blogspot.be/2014/10/appcompat-v21-material-design-for-pre.html http://android-developers.blogspot.be/2014/10/appcompat-v21-material-design-for-pre.html
Documentation: 文档:
http://developer.android.com/reference/android/widget/Toolbar.html http://developer.android.com/reference/android/widget/Toolbar.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.