简体   繁体   English

如何在android的操作栏中设置应用程序图标

[英]how to set app icon in action bar in android

i want set my app icon on my action bar please help me我想在我的操作栏上设置我的应用程序图标请帮助我

I have tried this but nothing created:我试过这个,但没有创建:

    getSupportActionBar().setTitle("MukilFM");
    getSupportActionBar().setIcon(R.drawable.fm);

Just create a separate layout.xml file for action bar and include it as a custom action bar in main.xml and set the main activity's theme to noTitle bar.只需为操作栏创建一个单独的 layout.xml 文件,并将其作为自定义操作栏包含在 main.xml 中,并将主活动的主题设置为 noTitle bar。 Here is my custome_app_bar.xml这是我的custome_app_bar.xml

<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
app:theme="@style/AppTheme"
android:padding="5dp"
>
    <ImageView
        android:id="@+id/acton_bar_imageView"
        android:background="@drawable/app_logo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"/>

and including in the main.xml is like that ....并包含在 main.xml 中就是这样......

    <include
    android:id="@+id/app_bar"
    layout="@layout/custome_app_bar"
    />

使用setLogo()方法。

getSupportActionBar().setLogo(R.drawable.fm);

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);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar.setLogo(R.drawable.fm);

And one more thing if you are using Activity then change to AppCompatActivity还有一件事,如果您正在使用Activity然后更改为AppCompatActivity

Try this way试试这个方法

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setLogo(R.drawable.fm);
            ab.setTitle("MukilFM");
            ab.setHomeAsUpIndicator(R.drawable.ic_menu);
            ab.setDisplayHomeAsUpEnabled(true);
        }
Objects.requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.whiteic);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM