[英]Android custom action bar
我有一个问题,就是我在应用程序中添加了自定义操作栏,但是操作栏视图中有一个间隙(链接图片中的红色圆圈位置)。
MainActivity代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
}
actionbar_layout代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#0146A3">
<TextView
android:id="@+id/abr_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="26sp"
android:text="Title"
tools:text="Title"/>
</RelativeLayout>
但不使用android支持库,则不会发生此问题。(“ MainActivity extands ActionBarActivity”更改为“ MainActivity extands Activity”,而“ getSupportActionBar()更改为getActionBar()”)
使用style.xml设置操作栏背景
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarItemBackground">@drawable/bg_actionbar_selected</item>
</style>
<style name="action_bar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/yourBackgound or color</item>
</style>
让我建议另一种方法:步骤1:隐藏操作栏。 使用此主题进行活动。
android:theme="@android:style/Theme.NoTitleBar"
步骤2:使用自定义相对布局作为操作栏
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#009ACD">
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.