我想显示一个带有自定义布局的操作栏。自定义布局有三个 ImageView,一个在中间,另外两个在 action bar 的左右两端。不得有后退按钮或操作项。
这是我用过的:
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(resource, null);
actionBar.setCustomView(actionBarView, lp);
自定义布局已正确展开,但未占据屏幕的整个宽度。这发生在 Android 4.4.2 (KitKat) 上。在 Gingerbread 上,上述安排工作正常。
我在以下讨论中尝试了推荐的解决方案:
- ActionBar - custom view with centered ImageView, Action Items on sides
- RelativeLayout/customview doesn't fill tab's width
但是问题依然存在。必须有一种方法可以强制布局占据整个屏幕宽度。
我正在使用 V7 AppCompat 库中的操作栏。有谁知道解决这个问题的方法吗??
编辑 1:
这是 XML 布局文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<ImageView
android:id="@+id/left_icon"
android:background="@drawable/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="leftButtonPressed"
android:hapticFeedbackEnabled="true"
/>
<ImageView
android:id="@+id/center_icon"
android:background="@drawable/centertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
/>
<ImageView
android:id="@+id/right_icon"
android:background="@drawable/rightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="rightButtonPressed"
android:hapticFeedbackEnabled="true"
/>
</RelativeLayout>
这是它在 Gingerbread (2.3.5) 上的样子:
如您所见,它充气正确。这是它在 Kitkat (4.4.2) 上的样子:
你可以看到右边有一点缝隙,布局没有占据完整的宽度。
我不认为布局有问题(它在 Android 2.3.5 上正确展开),但我可能是错的。如果我疯了,请告诉我。
编辑 2:
感谢Doctoror Drive和 this post ,我能够得到我想要的结果。我知道这有点奇怪:如果您没有任何导航或操作项,为什么要使用操作栏,但这是我解决特定问题所需要的。我有一个 TabHost,每个选项卡中都有一堆滑动 Fragment,当显示不同的 Fragment 时,顶部的 LinearLayout(即操作栏)需要更改其外观。
编辑 3:
这里有一些链接可以进一步理解这一点:
最佳答案
在我的例子中,我已经解决了绕过 LayoutInflater
调用的问题。
所以应该简单地替换这段代码:
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);
使用此代码:
actionBar.setCustomView(R.layout.custom_actionbar);
关于android - 具有自定义布局的 ActionBar 在 Android 4.4.2 (KitKat) 上不占据全屏宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23783381/