如何在工具栏中添加图像 [英] How to add image in toolbar

查看:89
本文介绍了如何在工具栏中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照示例立即创建我的应用我正在尝试在工具栏中添加图片,但是图像未显示,我正在尝试设置我的应用徽标,以下是我的代码和工具栏xml谁能告诉我这是什么错误?

I am following this example to create my app now i am trying to add image in my toolbar the but image is not displaying,i am trying to set my app logo,following is my code and toolbar xml can any one tell me what is mistake?

toolbar.xml

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/MyMaterialTheme.Base"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/toolbartitle"
        />

    </android.support.v7.widget.Toolbar>

MainActivity.java

MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

    private static String TAG = MainActivity.class.getSimpleName();

    private Toolbar mToolbar;
    private FragmentDrawer drawerFragment;
    private String chng;
    private Intent i;
    public SearchView searchView;
    public SearchManager searchManager;

    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    // Session Manager Class
    SessionManager session;

    private String id;
    private boolean b;
    private String rasa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        session = new SessionManager(getApplicationContext());
        id = getIntent().getStringExtra("id");
        System.out.println("MAIN ID : " + id);
       /* i=getIntent();
        chng=i.getStringExtra("Changes");*/

      //  Toast.makeText(getApplicationContext(),chng,Toast.LENGTH_LONG).show();
       mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
       getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout),mToolbar);
        drawerFragment.setDrawerListener(this);
        displayView(0);
    }

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

这是我的工具栏现在显示的方式

This how my toolbar shows right now

推荐答案

相反,您可以将图像设置为textview背景.在代码中使用android:background="@drawable/toolbartitle".和setTitle(").

instead you can set your image as textview background. use android:background="@drawable/toolbartitle". and setTitle("") in your code.

您最终的工具栏将如下所示

your final toolbar will look like

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@drawable/toolbartitle"
    local:theme="@style/MyMaterialTheme.Base"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" >


    </android.support.v7.widget.Toolbar>

这篇关于如何在工具栏中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆