2016-03-10 26 views
7

我想在标题前的android工具栏上显示imageview,但它不能正常工作,它在右侧标题后给出图像,任何人都可以帮助我希望图像处于在左侧工具栏 enter image description here在标题前的左上角的Android工具栏中显示imageview

这里的开始是我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.astro.famouspandit.Activities.Abstract.AbstractActivity"> 



     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="#0F6177" 
      app:popupTheme="@style/AppTheme.PopupOverlay" > 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageViewplaces" 
       android:src="@drawable/ic_question_mark" 
       /> 
      </android.support.v7.widget.Toolbar> 




</android.support.design.widget.CoordinatorLayout> 

而这里的活动

package com.astro.famouspandit.Activities.Abstract; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

import com.astro.famouspandit.Activities.Activity.ChartStyle; 
import com.astro.famouspandit.Activities.Activity.SelectLanguage; 
import com.astro.famouspandit.R; 

public class AbstractActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_abstract); 



     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


    } 


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


    } 

    public boolean onOptionsItemSelected(MenuItem item){ 
     int items = item.getItemId(); 
     switch(items){ 

      case R.id.action_Language:{ 
       Intent intent = new Intent(this,SelectLanguage.class); 
       startActivity(intent); 
      }break; 

      case R.id.action_CharStyle:{ 
       Intent intent = new Intent(this,ChartStyle.class); 
       startActivity(intent); 

      }break; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

回答

8

你可以尝试使用setIcon()方法。在您的布局文件从您的ToolbarImageView,并添加以下到您的Activity

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
getSupportActionBar().setIcon(R.drawable.ic_question_mark); 

编辑:您可能还需要包括getSupportActionBar().setDisplayShowHomeEnabled(true);

+2

我认为它仍然需要'getSupportActionBar() .setDisplayShowHomeEnabled(true);' –

+0

@PPartisan其工作但我怎么可以把imageview和标题 – bipin

+0

@ M.Sameer它的工作空间,但我怎么能把imageview和标题之间的空间 – bipin

1

Ĵ乌斯季增加一个TextView的ImageView

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="#0F6177" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/title" 
       android:text="title"/> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageViewplaces" 
       android:torightof="@id/title" 
       android:src="@drawable/ic_question_mark"/> 
    </RelativeLayout> 
</android.support.v7.widget.Toolbar> 
+0

我要去使用此为我所有的活动,这样我就不得不使用android:text =“title”来使用这个方法 – bipin

+0

,但是你需要自定义你的工具栏,所以你需要这样做 –

1

之前刚刚成立的layout_gravity = “左” 的ImageView的和layout_gravity = “中心” 的TextView。希望这有助于:

<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" android:layout_height="wrap_content" 
android:id="@+id/toolbar" 
android:background="?attr/colorPrimaryDark" 
android:minHeight="?attr/actionBarSize" 
android:fitsSystemWindows="true" 
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
> 
    <ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" 
    android:layout_gravity="left"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Title" 
    android:layout_gravity="center"/> 
</android.support.v7.widget.Toolbar> 
相关问题