【发布时间】:2015-07-09 05:06:44
【问题描述】:
我正在操作栏中设置自定义视图。使用 SDK 操作栏时,它会填充操作栏的宽度,但在使用 appcompat 版本时不会。为什么我的线性布局在使用 appcompat 和 Toolbar 时没有填满宽度?我该怎么办?
这是 pre-appcompat 的样子:
这就是 appcompat 的样子。 (红色显示我的自定义视图的范围。):
编辑:这是使用@Seidan 的显式布局参数提示的外观。 (在应该是白色的地方显示黑色文本):
这是我的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:background="#800"
>
<FrameLayout
android:id="@+id/actionbar_discard"
style="?attr/actionButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
>
<TextView
style="?android:actionBarTabTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_close_white_24dp"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:paddingRight="20dp"
android:text="@string/actionbar_cancel" />
</FrameLayout>
<FrameLayout
android:id="@+id/actionbar_done"
style="?attr/actionButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
style="?android:actionBarTabTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_check_white_24dp"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:paddingRight="20dp"
android:text="@string/actionbar_done" />
</FrameLayout>
</LinearLayout>
下面是我设置操作栏的方式:
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
ab.setCustomView(R.layout.actionbar_discard_done);
appcompat-v7:22.0.0 和新的 appcompat-v7:22.1.0 都存在此问题。
【问题讨论】:
-
你检查过这个答案stackoverflow.com/a/16652485/1880784吗?
-
嗯。这样可行!但现在我的文字样式错误。 (我现在必须扩充我的自定义视图,而不仅仅是传递 ID。我猜某些上下文或主题设置不正确。)
标签: android android-appcompat material-design