【问题标题】:Toolbar title with custom view带有自定义视图的工具栏标题
【发布时间】:2015-02-27 17:47:57
【问题描述】:

我正在尝试在我的工具栏中使用 setTitle 和自定义视图来显示标题。

我没有把它当作一个操作栏,而只是一个视图。

我在 Java 中同时添加标题和自定义视图

 toolbar = (Toolbar) view.findViewById(R.id.toolbar);

    if (title != null) {
        toolbar.setTitle(title);
        toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
    }

    if (subtitle != null) {
        toolbar.setSubtitle(subtitle);
        toolbar.setSubtitleTextColor(getResources().getColor(android.R.color.white));
    }

    // Add switch view to toolbar
    View switchView = LayoutInflater.from(getActivity())
            .inflate(R.layout.device_list_switch, null);
    toolbar.addView(switchView);

我的切换视图的 xml 是

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/discoverable"
    android:layout_alignParentBottom="true"
    android:layout_alignTop="@+id/discoverable"
    android:layout_toLeftOf="@+id/discoverable"
    android:layout_toStartOf="@+id/discoverable"
    android:gravity="center"
    android:text="@string/discoverable_switch_label"
    android:textColor="@android:color/white"
    />

<Switch
    android:id="@+id/discoverable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="@dimen/discoverable_switch_margin_left"
    android:layout_marginStart="@dimen/discoverable_switch_margin_left"/>

</RelativeLayout>

发生的情况是,RelativeLayout 填满了整个工具栏区域,并且标题不可见。

有什么想法吗?

【问题讨论】:

    标签: android android-5.0-lollipop toolbar


    【解决方案1】:

    如果您不将工具栏用作操作栏,那么也许您可以只使用布局。

    我使用了 LinearLayout 而不是 Toolbar。

    Toolbar 能给你带来什么好处?

    【讨论】:

    • 这是一个很好的建议。虽然使用工具栏可以让我轻松地扩充菜单
    • MenuInflater menuInflater = next MenuInflater(context); menuInflater.inflate(R.menu.main, menu);
    【解决方案2】:

    这可以通过在自定义视图旁边的工具栏中添加您自己的标题(和副标题,如果需要的话)文本视图来解决。

    但也许更好的方法是使用嵌套在第一个工具栏内的第二个工具栏。这样,所有格式都会为您处理好。

    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/topToolbar"
        android:layout_alignParentTop="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/titleToolbar"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                app:contentInsetStart="0dp"
                app:contentInsetLeft="0dp">
            </android.support.v7.widget.Toolbar>
    
            <CustomView... />
    
        </LinearLayout>
    
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      相关资源
      最近更新 更多