显示搜索图标的问题

时间:2017-09-29 08:18:05

标签: android android-toolbar android-search

我正在尝试在自定义工具栏上显示搜索图标!

问题: 我已将工具栏的标题设置为" Clubs List"在设计工具栏时,现在当我创建一个新的menu.xml文件以在工具栏上显示搜索图标时,虽然搜索图标显示在工具栏的右端但是带有工具栏标题("俱乐部列表&# 34;)即使我的黑色应用程序名称显示在左侧,如下图所示(" cric ..")

我只想" CLUBS LIST"并在工具栏上搜索图标,我该如何实现?

这是我的Toolbar.xml文件 enter image description here

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbarC"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/MyStyle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="60dp"
    android:fontFamily="serif-monospace"
    android:text="CLUBS LIST"
    android:textColor="#FFF"
    android:textSize="30sp"
    android:textStyle="bold" />


</android.support.v7.widget.Toolbar>

这是我的主要xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:layout="http://schemas.android.com/apk/res-auto"
android:background="#3dcc24">

 <include
    layout="@layout/clubs_tool_bar" />

 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </ListView>

</LinearLayout>

这是我的菜单xml文件

 <?xml version="1.0" encoding="utf-8"?>
 <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:title="search"
    android:icon="@drawable/ic_menu_search"
    app:showAsAction="collapseActionView|always"
    android:actionViewClass="android.widget.SearchView"
    android:theme = "@style/MyStyle" />
  </menu>   

这是我的Java文件

public class Clubs_List extends AppCompatActivity {
Toolbar toolbarC;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cricket_clubs);
    toolbarC = (Toolbar) findViewById(R.id.toolbarC);
    setSupportActionBar(toolbarC);
 }
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu,menu);
    return true;
}
}

这是我的styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" 
    parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="MyStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name = "android:textSize">30sp</item>



   </style>
</resources>

1 个答案:

答案 0 :(得分:0)

我可以知道您在清单中对活动应用了哪些主题?

你可能会给你的活动提供错误的主题以隐藏标题。

   <style name="MyTheme" parent="Theme.Design.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    </style>

在您的清单中,将MyTheme提供给您的活动。

    <activity
        android:name=".activities.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/MyTheme">

在您的活动中

   getSupportActionBar().setDisplayShowTitleEnabled(false);