gpt4 book ai didi

android - 如何将 Action 搜索图标添加到android中的工具栏

转载 作者:行者123 更新时间:2023-11-29 20:45:57 25 4
gpt4 key购买 nike

我在 Android studio 1.1 中工作,我试图在我的工具栏中包含一个搜索选项,该选项搜索显示在下拉菜单中,而不是像操作图标一样显示在工具栏中。我需要将图标搜索放在右侧工具栏中,靠近菜单选项的三个点。

这是我的xml Activity 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.xxxxx.myapplication.MainActivity" >

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:minHeight="?android:attr/actionBarSize"
android:background="#ff7fb4ff"

/>

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">

</LinearLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left" >

<ListView
android:id="@+id/mimenu"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:divider="#eee"
android:dividerHeight="1dp"
android:background="#ffa6d0ff" />

</RelativeLayout>

</android.support.v4.widget.DrawerLayout>

</LinearLayout>

在我的样式 xml 中

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

<item name="colorPrimary">#005500</item>

<item name="android:textColorPrimary">#FFFFFF</item>

<item name="android:textColorSecondary">#FFFFFF</item>



</style>

java类

public class MainActivity extends ActionBarActivity {

private CharSequence mTitle;

private SearchView mSearchView;
DrawerLayout drawerLayout;
ArrayAdapter<CharSequence> navigationDrawerAdapter;
ListView leftDrawerList;
String[] leftSliderData = new String[]{"uno","dos","tres"};
ActionBarDrawerToggle drawerToggle;

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

leftDrawerList = (ListView)findViewById(R.id.mimenu);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationDrawerAdapter= new ArrayAdapter<CharSequence>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);

toolbar.setNavigationIcon(R.drawable.ic_drawer);
//Toolbar will now take on default Action Bar characteristics


setSupportActionBar(toolbar);

drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close) {

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);

}

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);

}
};
drawerLayout.setDrawerListener(drawerToggle);

mTitle = getTitle();

getSupportActionBar().setDisplayShowTitleEnabled(false);


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

return true;

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

菜单代码

<menu 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" tools:context=".MainActivity"
android:id="@+id/menuMain"
>


<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search"

android:showAsAction="ifRoom"
/>

</menu>

最佳答案

在菜单代码中进行以下更改:android:showAsAction="always"

关于android - 如何将 Action 搜索图标添加到android中的工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com