【问题标题】:Titlebar get into ToggleButton in Toolbar标题栏进入工具栏中的切换按钮
【发布时间】:2017-10-21 14:15:13
【问题描述】:

我很想用这个代码在toolbar 中创建一个togglebutton

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#acacac">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#acacac"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentBottom="true"
            android:background="#202020" />
        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/check"
            android:layout_margin="10dp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOn=""
            android:textOff=""
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
</android.support.design.widget.AppBarLayout>

但是当我设置标题时,标题进入或与切换按钮合并:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
settitle("stackoverflow");

【问题讨论】:

  • 您希望它出现在左右或下方的哪个位置?
  • @Xenolion l当我设置一个小标题时我没有问题但是当我设置一个大标题时,标题文本像这样进入切换按钮i.imgur.com/pTTHtu0.png
  • 如果这个词必须像 stackoverflow bla...(这里的切换按钮) 看看它没有触及的三个点,你可以吗
  • @Xenolion 是的,我很满意,我唯一不希望它的标题与图片等切换按钮合并
  • 我现在给出一个解决方案,给我 2 分钟。

标签: android toolbar togglebutton titlebar


【解决方案1】:

Toolbar 不像旧的ActionBar 的好消息是工具栏易于定制。这就是我自定义您的布局的方式。首先,我在您的工具栏中添加了水平线性布局Linear Layout 作为视图。我已经删除了 ToggleButton 并将其添加到 LinearLayout 中,就在 TextView 之后,现在将成为您的应用程序标题。如果您不介意,您可以直接使用 XML 设置标题所以这是您的新 Toolbar 请先删除(或隐藏)您的 ToogleButtonToolbar 并将以下代码粘贴到您的 Toolbar 的位置是。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:lines="1"
            android:text="title here or even @string/Something"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/check"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOff=""
            android:textOn="" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

到目前为止,它在我的 Android Studio 中是这样的:
当您想将文本设置得更长时,您会看到点 (...),当您想要设置文本时,您会看到所有内容。

从现在开始,让我们谈谈您的代码中的更改。

您的Toolbar 标题将是您的TextView 中的值,您不再需要该行。

p>

toolbar.setTitle("stackoverflow");

您可以在xmlJava 代码中直接使用您的TextView。请记住,您也可以像普通布局一样增加TextSize。在代码中你可以使用

TextView text_title=(TextView)findViewById(R.id.title);
text_title.setText("stack Overflow");

编码愉快!

【讨论】: