【问题标题】:Custom layout in Toolbar工具栏中的自定义布局
【发布时间】:2015-10-18 06:22:14
【问题描述】:

最近我从旧的 ActionBar 迁移到了新的 Toolbar。

我有一个自定义活动(实际上是我的主要活动中的一个片段),我允许用户在其中单击工具栏中的图标,然后使用自定义布局出现一个搜索框(在工具栏中)。

问题是,在迁移之前它运行良好,但现在一旦我单击按钮并切换到带有搜索框的自定义操作栏布局,布局跨越所有屏幕,而不是工具栏的正常大小。

这是我用来替换工具栏的代码:

LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflator.inflate(R.layout.map_action_layout, null);

actionBar = ((DrawerActivity)getActivity()).getSupportActionBar();
actionBar.setCustomView(actionBarView);

这是地图操作布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
    android:id="@+id/autoComplete"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:hint="@string/search_adress_hint"
    android:imeOptions="actionSearch"
    android:inputType="textAutoComplete|textAutoCorrect"
    android:paddingRight="50dp"
    android:textColor="@android:color/white"
    android:textCursorDrawable="@null" >
    <requestFocus />
</AutoCompleteTextView>

<ImageButton
    android:id="@+id/clear_address"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="17dp"
    android:background="@drawable/ic_action_cancel"
    android:gravity="center_horizontal|center_vertical"
    android:scaleType="fitCenter" />

</RelativeLayout>

注意:无论我在上面的高度中设置什么值,无论是固定的dp值还是wrap_content,布局仍然跨越整个屏幕。

这是我的工具栏布局被替换:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_scrollFlags="scroll|enterAlways"
    android:elevation="4dp"
    app:layout_collapseMode="pin">

</android.support.v7.widget.Toolbar>

工具栏位于 AppBarLayout 中。

有人知道我做错了什么吗?我怎样才能达到我想要的结果?谢谢。

【问题讨论】:

    标签: java android android-actionbar toolbar material-design


    【解决方案1】:

    将工具栏高度设置为?attr/actionBarSize 是标准的。

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
    

    【讨论】:

    • 我的错!我累了,以为你想让我把它放在自定义布局中。对不起!这行得通!
    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多