我在ToolBar上使用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 :(得分:0)
编辑回答
SearchView是工具栏中的选项菜单项,您无法在其右侧显示自定义TextView,因为自定义视图始终位于选项菜单的左侧项目(参见Toolbar文档)。
所以,如果你想在SearchView的右边有一些文字,你可以添加另一个菜单项:
菜单/ 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);
}
}
这应该是这样的:
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显示在工具栏的右边缘。