简体   繁体   English

在Android的ActionBar中显示图标和标题

[英]Display icon and title in the ActionBar in Android

I want to display both icon and title in the action bar of an Activity. 我想在活动的操作栏中显示图标和标题。 Here is the screenshot of my app now: 这是我的应用程序的屏幕截图:

在此输入图像描述

Now I need the app logo be displayed to the left of MyApp text in the action bar. 现在我需要在操作栏中的MyApp文本左侧显示应用程序徽标。 I have already tried the following code to set the logo but nothing happened: 我已经尝试了以下代码来设置徽标但没有发生任何事情:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setLogo(R.drawable.ic_launcher_web);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        setContentView(R.layout.activity_main);

    }

I just tried with your code, and this works. 我刚试过你的代码,这很有效。

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);

Make sure your parent theme is set to Theme.AppCompat.Light.NoActionBar . 确保您的父主题设置为Theme.AppCompat.Light.NoActionBar

<!-- 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>

You can make a new layout for Action bar and you could implement in main activity 您可以为操作栏创建新布局,并且可以在主活动中实现
Create new Layout 创建新布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:minHeight="60dp"
android:id="@+id/topHeaderNav"
android:background="@color/primary"
android:layout_height="60dp"
tools:showIn="@layout/activity_main">

<ImageView
    android:layout_width="50dp"
    android:maxHeight="75dp"
    android:maxWidth="75dp"
    android:id="@+id/imgTop"
    android:layout_centerVertical="true"
    android:src="@drawable/mag"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/topTitleText"
    android:layout_toRightOf="@id/imgTop"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:text="Magnetic Orange"
    android:textColor="#FFFFFF"
     />

and implement it in Main Layout 并在主布局中实现它

 `<include layout="@layout/navbar"
    android:id="@+id/topHeaderNavMainActivity"
    />`

I think it will help you 我认为它会对你有所帮助

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);

Fist of all be sure you have used style with NoActionBar 所有的拳头都要确保你使用了NoActionBar风格

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

Set up your toolbar in onCreate() and be sure it's V7 onCreate()设置工具栏并确保它是V7

Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);

Your toolbar in xml looks like xml中的工具栏看起来像

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="60dp"/>

And use those methods you have previously tried. 并使用您之前尝试过的那些方法。 This will work. 这会奏效。

if your require to customize a action bar then use costume layout in action bar, following was the code which explain how customize the action bar, 如果您需要自定义操作栏然后在操作栏中使用服装布局,以下是解释如何自定义操作栏的代码,

In your activity put this code onCreate method 在您的活动中将此代码放在了创建方法上

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.actionbar_layout, null); //hear set your costume layout
        getSupportActionBar().setCustomView(v);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8731a0")));

it is the code which activiy inherit AppCompatActivity or if you are inheriting activity then replace getSupportActionBar() with getActionBar() and use. 它是活动继承AppCompatActivity的代码,或者如果您继承活动,则将getSupportActionBar()替换为getActionBar()并使用。

     private ViewGroup actionBarLayout;

      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_screen);
            context = this;

            actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_actionbar, null);

            actionBarLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            // Set up your ActionBar

    /* final ActionBar actionBar = getActionBar();*/

            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setCustomView(actionBarLayout);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.green)));

            init();
            event();
        }

and used following style.

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

    </style>

Try Custom ActionBar. 尝试自定义ActionBar。

custom_actionbar.xml custom_actionbar.xml

Layout XML 布局XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize" 
    android:clickable="true"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="1">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="15sp"
            android:background="@drawable/emaact"
            android:layout_alignParentBottom="true"
            android:layout_margin="10dp"
            android:id="@+id/imageView"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="right">

            <TextView
                android:layout_width="75dp"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Project"
                android:layout_gravity="center_vertical"
                android:textSize="20sp"
                android:id="@+id/textView" android:gravity="center_vertical"
                android:textStyle="bold"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.99"
            android:weightSum="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/txttitle"
                android:text="title"
                android:textStyle="normal"
                android:textSize="20sp"
                android:gravity="center"
                android:layout_weight="1.06"/>

        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menuline"
            android:layout_margin="10dp"
            android:id="@+id/slidingmenu"
            android:layout_gravity="center_vertical"/>

    </LinearLayout>
</RelativeLayout>

In Your MainActivity, Use This code. 在您的MainActivity中,使用此代码。

MainActivity.java MainActivity.java

public void customBar(){
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);

    LayoutInflater inflater = LayoutInflater.from(this);
    View customView = inflater.inflate(R.layout.custom_actionbar, null);

    TextView text =(TextView) customView.findViewById(R.id.txttitle);
    text.setText("Home");
    ImageView menus =(ImageView) customView.findViewById(R.id.slidingmenu);

    actionBar.setCustomView(customView);
    actionBar.setDisplayShowCustomEnabled(true);
}

Your Action Bar looks like... Custom ActionBar 您的操作栏看起来像... 自定义ActionBar

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

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