2015-09-16 16 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' की बजाय 'पृष्ठभूमि' विशेषता का उपयोग कर? –

+0

मैंने कोशिश की। यह ऐसा ही है। छवि में एंटर टूलबार क्षेत्र शामिल नहीं है। –

+0

'एंड्रॉइड का उपयोग करें: imageView के लिए layout_width = "match_parent" 'टूलबार के रूप में टूलबार' match_parent' है ताकि पूरे टूलबार पर छवि लागू करने के लिए। आपको ImageView चौड़ाई को 'match_parent' में संशोधित करना चाहिए। 'wrap_content' ImageView –

उत्तर

10

आप .setBackground(...) समारोह का उपयोग करने पर एक imageView के प्रयोग पर जोर देते हैं, तो कह सकते हैं।

आप Toolbar डालने एक ImageView इस तरह के साथ एक RelativeLayout उपयोग करने का प्रयास कर सकते हैं:

<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 की adjustViewBounds = "true" प्रॉपर्टी विंडो को फिट करने के लिए छवि को स्केल करेगी। इसका मतलब है कि आपकी स्रोत छवि की चौड़ाई और ऊंचाई के बीच संबंध रखा गया है, लेआउट_विड्थ को मिलान करने के लिए लेआउट_विड्थ को बढ़ाया जा रहा है इस मामले में स्क्रीन की चौड़ाई है – RWIL

2

निम्नलिखित मैं क्या चाहते है एक तत्व बुद्धि जोड़ने के बजाय टूलबार में छवि छवि को टूलबार की पृष्ठभूमि, नीचे कोड के रूप में जोड़ें।

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

आप जावा कोड में इसे बदलना चाहते हैं, तो आप toolbar.setBackground(ContextCompat.getDrawable(yourcontext, R.drawable/puri);

+0

में अपेक्षाकृत मेनू आइटम और बाल दृश्य रखने के लिए मैं उस तरह से जोड़ सकता हूं। मैं जानता था कि। लेकिन मैं इसे imageview का उपयोग करके जोड़ना चाहता हूं। मैं छवि को एक अलग दृश्य होना चाहता हूं। ताकि मैं आसानी से फसल और स्केलिंग का उपयोग कर सकूं –

+0

ठीक है, क्षमा करें। ImageViews के लिए आप 'scaleType' विशेषता' मान 'centerCrop', या किसी अन्य मान के साथ सेट कर सकते हैं, क्या आपने इसे पहले से ही आजमाया है? और, आप '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> 

संबंधित मुद्दे