2015-08-22 9 views
12

टूलबार में किसी गतिविधि के शीर्षक को इस तरह से कैसे केंद्रित करें कि यह टूलबार बैक बटन के साथ भी काम करता है?टूलबार में शीर्षक को कैसे केंद्रित करें (यहां तक ​​कि टूलबार बैक बटन के साथ भी प्रदर्शित)

वर्तमान में, मैंने पाया है कि सबसे अच्छा समाधान 60 डीपी मार्जिन था यदि बैक बटन प्रदर्शित होता है।

+0

मुझे लगता है कि आप अपनी कार्रवाई बार के लिए एक कस्टम दृश्य बनाने के लिए की आवश्यकता होगी। –

+0

मैंने लेआउट_ग्रैविटी सेट सेंटर पर सेट टूलबार व्यू में टेक्स्ट व्यू जोड़ने का प्रयास किया है। जब टूलबार बैक बटन प्रदर्शित होता है तो यह काम नहीं करता है। –

+0

आप एक कस्टम व्यू (एक बटन (बैक) और एक टेक्स्टव्यू (शीर्षक) युक्त क्षैतिज रैखिक लेआउट बना सकते हैं और उन्हें केंद्र बना सकते हैं। –

उत्तर

2

मुझे नहीं पता कि सबसे अच्छा अभ्यास कैसा है, लेकिन आप टूलबार के अंदर कस्टम बैक बटन का उपयोग करके इस वर्कअराउंड को आजमा सकते हैं। लेआउट कोड:

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp"> 

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

     <ImageView 
      android:id="@+id/toolbar_back_button" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="20dp" 
      android:src="@drawable/ic_back" /> 

     <TextView 
      android:id="@+id/toolbar_title" 
      style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" /> 

    </RelativeLayout> 

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

और गतिविधि में कोड:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_title); 
    setSupportActionBar(toolbar); 

    ImageView backButton = (ImageView) findViewById(R.id.toolbar_back_button); 
    backButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      finish(); 
     } 
    }); 
4

ही वापस बटन का उपयोग कर, यह मेरे लिए काम किया। एक्सएमएल में:

<android.support.v7.widget.Toolbar 
    android:id="@+id/tb_title_container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white"> 

    <TextView 
     android:id="@+id/tv_title" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:textAllCaps="true" 
     android:textColor="#2B2B2B" 
     android:textSize="14sp" /> 

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

जावा कोड में:

int contentInsetStartWithNavigation = mTitleContainer.getContentInsetStartWithNavigation(); 
mTitleContainer.setContentInsetsRelative(0, contentInsetStartWithNavigation); 
+0

इस काम की पुष्टि कर सकते हैं महान काम करता है। – Travis

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