[英]Android actionbar icon
I would like to add an icon in status bar. 我想在状态栏中添加一个图标。 I tried- 我试过了-
getActionBar().setIcon(R.drawable.my_icon); //crashes app
getSupportActionBar().setIcon(R.drawable.ic_statusbar_msg); //not working
<activity android:icon="@drawable/my_icon" /> //also not working
<application android:logo="@drawable/Image"> //still not working
Icon is not shown on action bar. 图标未显示在操作栏上。 Any idea? 任何想法?
Try this code. 试试这个代码。 Import v7.widget.Toolbar and extends AppCompatActivity 导入v7.widget.Toolbar并扩展AppCompatActivity
import android.support.v7.widget.Toolbar;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_statusbar_msg);
Use Toolbar instead of actionBar. 使用工具栏而不是actionBar。 for this you have to change your style.xml like this: 为此,您必须像这样更改style.xml:
<style name="YourThemeName" parent="Theme.AppCompat.Light.NoActionBar">
and add Toolbar to your activity layout. 并将工具栏添加到您的活动布局。 and in your oncreate method in activity do this: 并在活动的oncreate方法中执行以下操作:
toolbar = (Toolbar) findViewById(R.id.toolbarId);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(context.getResources().getColor(R.color.white));
getSupportActionBar().setTitle("title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.navigation_right); // change back button image
getSupportActionBar().setIcon(R.drawable.your_icon);
Don't use setIcon. 不要使用setIcon。 On some images it sets padding or etc. Instead you can extend AppCompatActivity, set the theme to a NoActionBar one, and add a toolbar in your layout file in which you put the image you want. 在某些图像上,它设置填充或其他内容。相反,您可以扩展AppCompatActivity,将主题设置为NoActionBar,然后在布局文件中添加工具栏,在其中放置所需的图像。 Then you get a reference to the toolbar as @vinoth12594 shows you. 然后,您将获得对工具栏的引用,如@ vinoth12594所示。 More details here . 更多细节在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.