Android 如何在操作栏的右侧添加图像视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21178860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add imageview on right hand side of action bar?
提问by anuruddhika
I want to add image view on action bar right hand side.I tried to do that but i couldn't. Can anyone help me?
我想在操作栏右侧添加图像视图。我试图这样做,但我不能。谁能帮我?
This is my image view xml
这是我的图像视图 xml
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/adnace_search_i" />
This is the image of action bar that i want to create.
这是我要创建的操作栏的图像。
回答by Gopal Gopi
try this...
尝试这个...
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.adnace_search_i);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
回答by Mandar Kakade
Use
用
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_imageview, null);
actionBar.setCustomView(v);
Code for Custom ImageView
自定义 ImageView 的代码
<RelativeLayout 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:id="@+id/layout">
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/adnace_search_i" />
</RelativeLayout>
回答by Alfaplus
You don't need an ImageView for this. Using a new item using Icon for your image will be the smartest solution.
为此,您不需要 ImageView。使用图标为您的图像使用新项目将是最聪明的解决方案。
Add this item to your menu.xml:
将此项目添加到您的 menu.xml 中:
<item
android:title="@string/action_scanner"
android:orderInCategory="79"
android:icon="@drawable/adnace_search_i"
android:showAsAction="ifRoom|withText" />
Instantiate the menu in onCreateOptionsMenu
and implement it in onOptionsItemSelected
.
在 中实例化菜单onCreateOptionsMenu
并在 中实现它onOptionsItemSelected
。
回答by Mukul Aggarwal
Add this line to the menu.xml in the res/menu/menu.xml
将此行添加到 res/menu/menu.xml 中的 menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
You can see more actionBar property over android developer tutorials
您可以在android 开发者教程中看到更多 actionBar 属性
回答by Beast77
Use this layout. I just used that on API 28
使用这种布局。我刚刚在 API 28 上使用了它
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:onClick="makeToast"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_flag" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>