我的要求是增加标题栏的高度,但是我不确定这是否可行。如果可以的话,我该如何增加它的高度,以便有更多的空间。如果不是,则需要在自定义标题栏中添加菜单选项(汉堡式)。
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
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);
}
像这样更改您的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);
}
例如:现在,您的操作栏将显示如下:
然后,如果您需要setting menu
在操作栏中添加项目。
将以下代码添加到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);
}
}
然后在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>
现在,您可以在操作栏中看到设置图标
最后,如果您必须增加操作栏的高度:
在清单中:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > --->set a theme
...
</application>
然后在Styles.xml中:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarSize">80dip</item>
</style>
</resources>
现在,操作栏的大小已增加到我们在样式中指定的大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句