2015-09-16 3 views
8

背景デザインを使用してツールバーを作成する方法を教えてください。アンドロイドツールバー:背景に画像を含むツールバーとメニュー項目を表示する方法

toolbar with image and overflow and search menu icon.

私は次のコードをしようとしています:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/actionBar" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    app:contentInsetEnd="0dp" 
    app:contentInsetStart="0dp" 
    android:minHeight="200dp"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageViewplaces" 
     android:src="@drawable/puri" 
     android:layout_gravity="center"/> 

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

と私は何を取得していますが、次のとおりです。

enter image description here

+0

'src'ではなく 'background'属性を使用していますか? –

+0

私は試しました。それは同じだ。イメージはアンカーツールバー領域をカバーしません。 –

+0

ImageViewのツールバーとして 'android:layout_width =" match_parent "を' match_parent'にすると、ツールバーの上にイメージを適用できます。 ImageViewの幅を 'match_parent'に変更する必要があります。 'wrap_content'はImageViewで提供されている画像に従って画像の幅と高さを表示します –

答えて

10

を呼び出すことができます。

あなたはこのようImageViewを重ねるToolbarRelativeLayoutを使用して試すことができます:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/imageViewplaces" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:src="@drawable/YOUR_IMAGE" /> 

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/toolbar_actionbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

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


</RelativeLayout> 
+0

ツールバーの高さはどこですか? –

+0

ImageViewのandroid:adjustViewBounds = "true"プロパティは、ウィンドウに合わせて画像を拡大/縮小します。これは、ソースイメージの幅と高さの関係が保持されていることを意味し、layout_widthがmatch_parentに伸びている間、この場合の画面の幅 – RWIL

2

は、私が何をしたいです要素ウィットを追加する代わりにhツールバーへの画像は、ツールバーの背景として画像を追加します(下のコードを参照)。

<android.support.v7.widget.Toolbar  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/actionBar" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    app:contentInsetEnd="0dp" 
    app:contentInsetStart="0dp" 
    android:background="@drawable/puri" 
    android:minHeight="200dp"> 

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

Javaコードでそれを変更したい場合は、.setBackground(...)機能を使用して上のImageViewのを使う、という場合は、toolbar.setBackground(ContextCompat.getDrawable(yourcontext, R.drawable/puri);

+0

を追加できます。私はそれを知っていた。しかし、私はそれをimageviewを使って追加したい。私はイメージを別個のビューにしたい。だから私は簡単に作物とスケーリングを使用することができます –

+0

おかげで、申し訳ありません。 ImageViewsの場合は、 'centerCrop'という値を持つ' scaleType'属性を設定することができます。もう一つの値は既に試しましたか?また、なぜ 'layout_width'を' wrap_content'に設定していますか? – yennsarah

0

より適切な解決策がAppBarLayoutを使用することです:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbarlayout" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    > 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="?android:attr/actionBarSize" 
     android:src="@drawable/bg" 
     android:scaleType="centerCrop"/> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 
    </RelativeLayout> 
</android.support.design.widget.AppBarLayout> 

関連する問題