2017-10-21 341 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"); 

快樂編碼!