2015-11-25 2 views
11

मैं इस लक्ष्य को हासिल करने के लिए कोशिश कर रहा हूँ के अधिकार के लिए कस्टम दृश्य जोड़ें:(लाल रंग की पृष्ठभूमि के साथ <code>toolbar</code> में एक टाइमर) उपकरण पट्टी

A timer in the toolbar along with red background

मैं toolbar में customView जोड़ने की कोशिश कर रहा हूँ। यह हमेशा पीछे तीर के बगल में चरम बाएं होने पर समाप्त होता है। नीचे दी गई छवि में YP टेक्स्ट की तरह एक कस्टम Textview टूलबार में जोड़ा गया है। enter image description here

toolbar के लिए कोड:

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

लेआउट जोड़ने के लिए कोड:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:gravity="end" 
       android:layout_height="wrap_content"> 
    <TextView android:layout_width="wrap_content" 
       android:text="yp" 
       android:layout_height="wrap_content"/> 
</LinearLayout> 

मैं:

LayoutInflater mInflater= LayoutInflater.from(context); 
    View mCustomView = mInflater.inflate(R.layout.textview, null); 
    toolbar.addView(mCustomView); 

यह लेआउट मैं परीक्षण के लिए अभी द्वारा जोड़ा जा रहा है कुछ हद तक याद आ रही है, पता नहीं लगा सका। मेरे बाल खींच कर बाहर।

+0

बदलें LinearLayout के '' match_parent' को layout_width' काम किया। –

उत्तर

16

को R.layout.textview बदलते हैं, तो अंदर Toolbar

  <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/toolbar_height" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

      <RelativeLayout.... 

      </RelativeLayout> 

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

जब मैं ऐसा कुछ करता हूं, हालांकि, मैंने जो टेक्स्ट व्यू जोड़ा है उसे देखा है लेकिन यह अब टूलबार शीर्षक नहीं दिखाता है। कोई विचार क्यों? – AdamMc331

+0

क्या आपने 'getSupportActionBar() सेट किया है। SetDisplayShowTitleEnabled (false); '? या 'टेक्स्ट व्यू' टूलबार शीर्षक को ओवरलैड कर सकता है। –

+0

हां क्योंकि टूलबार शीर्षक के बाईं ओर 'RelativeLayout' जोड़ा जाता है। विशेष रूप से यदि आप 'alignParentRight = "true" जोड़ते हैं, तो यह शीर्षक को देखने के बाहर धक्का देता है।मैंने इस मुद्दे को हल करने के लिए एक और जवाब जोड़ा है, उस कोड के साथ एक टिप्पणी बहुत लंबी होगी। –

0

इसे आज़माएं।

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

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TV" 
        android:id="@+id/textView2" 
        android:layout_centerVertical="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentEnd="true" /> 
      </RelativeLayout> 
7

को देखते हुए उत्तर काम ViewGroups जोड़ सकते हैं, लेकिन आप उपकरण पट्टी के लिए एक शीर्षक (सबसे अधिक संभावना आप करेंगे) स्थापित करने के लिए चाहते हैं, तो जोड़ने RelativeLayout शीर्षक को दाईं ओर देखने के लिए शीर्षक को धक्का देता है। इस मामले में आपको getSupportActionBar().setDisplayShowTitleEnabled(false) सेट करके टूलबार शीर्षक को अक्षम करने की आवश्यकता है और टूलबार में जोड़े जाने वाले दूसरे दृश्य के बगल में कस्टम टेक्स्ट व्यू जोड़ें। तो एक उदाहरण के रूप में आप एक ImageView इस तरह से जोड़ना होगा:

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

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

     <TextView 
      android:id="@+id/custom_toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      style="@style/ActionBarTitle"/> 

     <ImageView 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:scaleType="centerCrop" 
      android:adjustViewBounds="true" 
      android:layout_centerVertical="true" 
      android:layout_toRightOf="@id/custom_toolbar_title" 
      android:src="@drawable/arrow"/> 

    </RelativeLayout> 

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

उन लोगों के लिए अच्छी तरह से काम करता है, जिन्हें 'style = "@ style/actionBarTitle" की आवश्यकता है, वहां 'style =" @ style/textAppearance.AppCompat.Widget.ActionBar.Title "' –

2

बस एक ही मुद्दा था, लेकिन मैं एक सरल पाया समाधान (मेरी विनम्र राय में)।

आप थोड़ा से अपने कोड बदल सकते हैं:

LayoutInflater mInflater= LayoutInflater.from(context); 
View mCustomView = mInflater.inflate(R.layout.textview, null); 
toolbar.addView(mCustomView); 

करने के लिए:

LayoutInflater mInflater= LayoutInflater.from(context); 
View mCustomView = mInflater.inflate(R.layout.textview, null); 
toolbar.addView(mCustomView, new Toolbar.LayoutParams(Gravity.RIGHT)); 

दृश्य तो गुरुत्वाकर्षण सही करने के लिए सेट के साथ जोड़ा जाता है!

5

मैं सिर्फ right को layout_gravity सेट और यह

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="0dp" 
    android:layout_height="?android:actionBarSize" 
    app:title="NEW REQUESTS" 
    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="blahblah" 
     android:layout_gravity="right" 
     /> 
    </android.support.v7.widget.Toolbar> 
+0

मैं वास्तव में काम नहीं कर सकता! जैसा। टूलबार के अंदर TextView के विकल्प के रूप में layout_gravity नहीं देता है, इसलिए मैंने इसे आजमाया नहीं था। – Anarchofascist

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