2017-10-21 346 views
0

我正在tyring在toolbar创建togglebutton与此代码: enter image description here标题栏进入切换按钮在工具栏

<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"); 
+1

你想让它出现在左边还是右边? – Xenolion

+0

@ Xenolion当我设置一个小标题我没有问题,但是当我设置一个大标题,标题文本进入togglebutton像这样 https://i.imgur.com/pTTHtu0.png –

+1

那么你好吗如果这个词必须像**堆栈溢出bla ...(切换按钮在这里)**看到它不触及的三个点 – Xenolion

回答

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工作室至今:
当你要设置你的文字比较长,你会看到点(...)在其较小的你看到它所有。

Showing Longer words cropped

从现在一个可以让六方会谈有关在代码中的变化。

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

toolbar.setTitle("stackoverflow"); 

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

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

快乐编码!