2016-11-23 70 views
0

我使用的TextView工具欄上的TextView的行動,我想設置正確的這個TextView的與搜索查看留如何使用工具欄上的

我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:anroid="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_search_result" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    app:titleTextColor="@android:color/holo_blue_bright" > 

    <TextView 
     anroid:id="@+id/toolbarTextView" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:visibility="gone" 
     android:text="FILTER" 
     android:textColor="@color/whiteColor" /> 
</android.support.v7.widget.Toolbar> 

<TextView 
    android:id="@+id/text_view_result" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="test result" /> 
</LinearLayout> 

當我建,TextView的沒有出現在ToolBar的右側,它只是出現在SearchView的EditText中。

如何解決這個問題?謝謝

+0

爲什麼要添加textview而不改變工具欄的標題? –

回答

0

EDITED ANSWER

一個搜索查看是在工具欄選項菜單項,你不能有一個自定義的TextView到它的權利,因爲自定義視圖是總是向左選項菜單項(請參閱Toolbar文檔)。

所以,如果你想要一些文本的搜索查看的權利,你可以像下面這樣添加另一個菜單項:

菜單/ main.xml中:

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <item 
     android:id="@+id/search" 
     android:icon="@android:drawable/ic_menu_search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     android:title="Search" 
     app:showAsAction="always" /> 

    <item 
     android:id="@+id/filter" 
     android:title="FILTER" 
     app:showAsAction="always" /> 

</menu> 

佈局/ activity_main.xml中:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_search_result" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:titleTextColor="@android:color/holo_blue_bright" /> 

    <TextView 
     android:id="@+id/text_view_result" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="test result" /> 

</LinearLayout> 

MainActivity:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar t = (Toolbar) findViewById(R.id.toolbar_search_result); 
     setSupportActionBar(t); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == R.id.filter) { 
      // Do here whatever you want if FILTER is clicked 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

這將導致這樣的事情:

Search collapsed Search expanded

OLD ANSWER

嘗試修改您的TextView這樣的:

<TextView 
     anroid:id="@+id/toolbarTextView" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="right" 
     android:text="FILTER" 
     android:textColor="@color/whiteColor" /> 

TextView的不出現,因爲你設置了android:visibility="gone"

此外,您應該使用wrap_content而不是match_parent爲TextView不包括工具欄的標題。

android:layout_gravity="right"會導致TextView顯示在工具欄的右邊緣。

+0

我已經代替了android:layout_gravity =「right」,並且它已經出現,但是當查看視圖展開時,TextView仍然保留 –

+0

如果您的工具欄中有一個SearchView,那麼TextView將始終位於其左側,不能將其放在SearchView的右側。但是,您可以在SearchView的右側添加另一個菜單項和所需的文本,請參閱編輯的答案。 – weibeld

+0

你的完美答案,它的工作,非常感謝! :) –