【问题标题】:How to add image in toolbar如何在工具栏中添加图像
【发布时间】:2015-10-23 11:13:52
【问题描述】:

我正在关注this 示例来创建我的应用程序现在我正在尝试在我的工具栏中添加图像但图像未显示,我正在尝试设置我的应用程序徽标,以下是我的代码和工具栏 xml 可以是任何一个告诉我什么是错误?

工具栏.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

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;
    }

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

【问题讨论】:

  • @Blackbelt 我不是在谈论汉堡包图标..我想要图像而不是标题

标签: android android-imageview android-toolbar


【解决方案1】:
  <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/color_blue"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageView
                android:id="@+id/tv_header_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/header_title" />
        </android.support.v7.widget.Toolbar>

你也可以通过这个方法设置按钮的事件...

 public static void showToolBar(Toolbar toolbar,
                                   final AppCompatActivity activity) {
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
        @SuppressWarnings("deprecation")
        Drawable drawable = activity.getResources().getDrawable(
                R.mipmap.back_icon);
        drawable.setColorFilter(
                activity.getResources().getColor(R.color.color_white),
                android.graphics.PorterDuff.Mode.SRC_ATOP);
        activity.getSupportActionBar().setHomeAsUpIndicator(drawable);
        toolbar.setBackgroundColor(activity.getResources().getColor(
                R.color.color_blue));
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

【讨论】:

  • getDrawable 自 API22 起已弃用
  • 在 28 版中不允许在 Toolbar 中包含 ImageView。
  • 你可以放任何意见@Kostadin
【解决方案2】:

使用操作栏上的徽标显示。

    getSupportActionBar().setLogo(R.drawable.logo);
    getSupportActionBar().setDisplayUseLogoEnabled(true);

【讨论】:

  • 它可以显示在工具栏的中心并且标题移动到右侧
  • 我正在使用这种方法,但是图像(徽标)现在位于工具栏的中心并且标题消失了。如何将徽标移到右侧(结束),将标题移到左侧(开始)?
【解决方案3】:

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

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

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

【讨论】:

  • 我是说使用图像作为工具栏背景
  • 不使用imageview怎么设置图片??
  • 我已将您的图片设置为工具栏背景...阅读答案,然后尝试,如果不起作用,请发表评论
  • 我使用了您的方法并将 setTitle("") 设置为工具栏,但标题中仍然有我的项目名称。我搜索并删除了它,现在我在标题中获得了包名......
  • 我成功删除了app:title=" "的标题;注意空间
【解决方案4】:

这是我犯的错误,我正在使用自定义工具栏 xml 进行检查,但我的 activity_main.xml 中已经有工具栏..希望我的回答也能对其他人有所帮助

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/MyToolBarStyle.Base"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/toolbartitle"
            android:minHeight="?attr/actionBarSize" />
    </LinearLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <!-- activity view -->
        <!-- Framelayout to display Fragments -->
        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- navigation drawer -->
        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

【讨论】:

    【解决方案5】:

    你应该设置image src属性而不是设置imageview背景drawable。

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

    【讨论】:

    • 你的drawable是一个有效的资源吗?
    • 什么是有效资源?
    • 它是在编辑器中打开时可以看到的 PNG 图像吗?您实际上是否在 activity_main.xml 中包含了工具栏?
    • 是的,它是 png 图像..并在我的问题中看到,更多我已经提到了我的示例的链接
    • 发布activity_main.xml
    【解决方案6】:

    你错过了显示来源

    android:src="@drawable/yourImage"
    

    如果不起作用,请尝试添加一些真正的宽度和高度,而不是包装内容

    android:layout_width="50dp"
    android:layout_height="50dp"  
    

    【讨论】:

    • 我已经尝试过使用 src 没有改变。现在请参阅我编辑的代码问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多