我正在尝试使用带有自定义 View 的简单操作栏,但我得到以下结果:
为了演示,我创建了一个带有黄色背景颜色的简单 xml,它应该占据整个宽度。
这里是xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/yellow"
android:orientation="vertical" >
<TextView
android:visibility="gone"
android:id="@+id/action_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/action_save"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textColor="@android:color/white"
android:text="@string/save" />
<ImageView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_actionbar_logo" />
</RelativeLayout>
我的应用程序使用以下主题:
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>
这是actionbar的初始化代码:
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.actionbar);
最佳答案
解决方案是添加:
actionBar.setDisplayShowTitleEnabled(false);
代码应该类似于:
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.actionbar);//set the custom view
关于Android 4.4 Kitkat 自定义 View 操作栏未填充整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172256/