14

डिजाइन समर्थन पुस्तकालय संस्करण 25BottomNavigationView, हर समय

compile 'com.android.support:design:25.0.0' 

<android.support.design.widget.BottomNavigationView 
     android:id="@+id/bottomBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_gravity="center" 
     app:itemBackground="@color/colorPrimary" 
     app:menu="@menu/bottom_navigation_main" 
     android:forceHasOverlappingRendering="true"/> 

जब @ मेनू/bottom_navigation_main में केवल तीन कार्यों से देखते हैं मैं android.support.design.widget.BottomNavigationView उपयोग कर रहा हूँ में दोनों माउस और पाठ लेबल प्रदर्शित यह हर समय आइकन और टेक्स्ट लेबल दोनों प्रदर्शित करता है।

तीन से अधिक कार्रवाइयां होने पर हर समय आइकन और टेक्स्ट लेबल प्रदर्शित करने का तरीका क्या है।

+0

अपने bottom_navigation_main.xml मेनू में,: इस Kotlin कोड का प्रयास करें showAsAction = "ifRoom" एंड्रॉयड के लिए इसे बदल: showAsAction = "हमेशा" प्रत्येक आइटम के लिए। –

+0

नहीं, यह काम नहीं किया। मैंने पहले कोशिश की थी। –

+0

क्या आप अपना मेनू एक्सएमएल फ़ाइल –

उत्तर

4

BottomNavigationView कक्षा में एक BottomNavigationMenuView फ़ील्ड है और BottomNavigationMenuView में एक BottomNavigationItemView [] फ़ील्ड है, जो नीचे बार में आइटम है।

कहें एन आइटम की संख्या है, BottomNavigationMenuView BottomNavigationItemView [] सरणी के प्रत्येक सदस्य पर BottomNavigationItemView.setShiftingMode (n> 3) को कॉल करेगा। यह फ़ंक्शन व्यवहार का निर्णय लेता है (हमेशा शीर्षक दिखाएं या केवल चयन पर)।

इसलिए हमेशा शीर्षक दिखाने का तरीका इस विधि को कॉल करने का प्रयास करना है और आप निजी फ़ील्ड तक पहुंचने के लिए प्रतिबिंब का उपयोग कर सकते हैं।

BottomNavigationView bottomNavigationView= (BottomNavigationView) findViewById(R.id.bottom_navigation); 


// get the private BottomNavigationMenuView field 
     Field f = null; 
     try { 
      f = bottomNavigationView.getClass().getDeclaredField("mMenuView"); 
     } catch (NoSuchFieldException e) { 
      e.printStackTrace(); 
     } 
     f.setAccessible(true); 
     BottomNavigationMenuView menuView=null; 
     try { 
      menuView = (BottomNavigationMenuView) f.get(bottomNavigationView); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } 

// get the private BottomNavigationItemView[] field 
     try { 
      f=menuView.getClass().getDeclaredField("mButtons"); 
     } catch (NoSuchFieldException e) { 
      e.printStackTrace(); 
     } 
     f.setAccessible(true); 
     BottomNavigationItemView[] mButtons=null; 
     try { 
      mButtons = (BottomNavigationItemView[]) f.get(menuView); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } 


     for(int i=0;i<mButtons.length;i++){ 
      mButtons[i].setShiftingMode(false); 
      mButtons[i].setChecked(true); 
     } 
+0

यह बहुत अच्छा है, बस हमें यह सुनिश्चित करने की ज़रूरत है कि BottomNavigationMenuView भी स्थानांतरित नहीं होता है। -> एफ = menuView.getClass()। GetDeclaredField ("mShiftingMode"); \t \t \t f.setAccessible (true); f.setBoolean (menuView, false); –

13

यह संस्करण 25

इस कोड का प्रयास करें में मुश्किल है। लेकिन मुझे लगता है कि यह अच्छा समाधान नहीं है।

BottomNavigationView navigationView = (BottomNavigationView) findViewById(R.id.bottomBar); 
BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigationView.getChildAt(0); 
for (int i = 0; i < menuView.getChildCount(); i++) { 
    BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i); 
    itemView.setShiftingMode(false); 
    itemView.setChecked(false); 
} 
+0

एंड्रॉइड स्टूडियो में, इस तरह कोड जोड़ना चाहिए: '' // noinspection प्रतिबंधित प्रतिबंधित एपीआई itemView.setShiftingMode (false); // noinspection प्रतिबंधित एपीआई itemView.set चेक (गलत); '' ' –

+3

यह अभी भी आइटम बदलता है – CodeToLife

+1

बिल्कुल सही !! आइकन और टेक्स्ट दोनों प्रदर्शित करता है। लेकिन स्थानांतरण मोड (झूठा) काम नहीं कर रहा है। – Minkoo

4

क्या आप इस प्रभाव को चाहते हैं?

Click here to view the image

यदि हां, तो मैं सिफारिश की आप BottomNavigationViewEx कोशिश करने के लिए

+1

आपकी लाइब्रेरी अच्छी और प्रभावशाली काम है लेकिन मैं डिज़ाइन लाइब्रेरी का उपयोग करके इस कार्यक्षमता को प्राप्त करने के लिए देख रहा था 25.0.0 दुर्भाग्य से यह एंड्रॉइड डिज़ाइन अभ्यास –

+1

https://material.google.com/components/bottom-navigation.html के खिलाफ है –

+0

यह [डॉक्स] में "फिक्स्ड नीचे नेविगेशन बार" के अनुसार, सामग्री डिजाइन चश्मा के खिलाफ नहीं है (https://material.io/guidelines/components/bottom-navigation.html)। इसके अलावा, इस भयानक पुस्तकालय को साझा करने के लिए मेरा व्यक्तिगत धन्यवाद। –

3

मैं BottomNavigationView के साथ कुछ अजीब व्यवहार किया था। जब मैं इसमें कोई आइटम/टुकड़ा चुन रहा था, तो टुकड़ा नीचे की तरफ देखता है, नीचे की तरफ देखें, इसलिए नीचे दिए गए टेक्स्ट का स्क्रीन स्क्रीन के नीचे चला जाता है, इसलिए केवल आइकन दिखाई दे रहे थे और टेक्स्ट किसी भी आइटम पर क्लिक करने पर छिपा हुआ था।

यदि आप उस अजीब व्यवहार का सामना कर रहे हैं तो यहां समाधान है। बस टुकड़ा की अपनी जड़ लेआउट में

android:fitsSystemWindows="true" 

हटाने । बस इसे हटाएं और उछाल! BottomNavigationView ठीक काम करेगा, अब इसे टेक्स्ट और आइकन के साथ दिखाया जा सकता है। मेरे पास यह रूट रूट समन्वयक के टुकड़े में था।

इसके अलावा मोड स्थानांतरण निष्क्रिय करने के लिए अपनी गतिविधि में

BottomNavigationViewHelper.disableShiftMode(bottomNavigationView); 

जोड़ने के लिए मत भूलना।

public class BottomNavigationViewHelper { 

@SuppressLint("RestrictedApi") 
public static void removeShiftMode(BottomNavigationView view) { 
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0); 
    try { 
     Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode"); 
     shiftingMode.setAccessible(true); 
     shiftingMode.setBoolean(menuView, false); 
     shiftingMode.setAccessible(false); 
     for (int i = 0; i < menuView.getChildCount(); i++) { 
      BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i); 
      item.setShiftingMode(false); 
      // set once again checked value, so view will be updated 
      item.setChecked(item.getItemData().isChecked()); 
     } 

    } catch (NoSuchFieldException e) { 
     Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field"); 
    } catch (IllegalAccessException e) { 
     Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode"); 
    } 
} 

}

+0

STAR_ZERO के उत्तर के संयोजन में यह मेरी समस्या हल हो गया! –

2

खिताब सभी तरह दिखाने के लिए:

यहाँ कि वर्ग है। अगर आप एंड्रॉयड है

@SuppressLint("RestrictedApi") 
override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_ofree) 

    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener) 

    val menuView = navigation.getChildAt(0) as BottomNavigationMenuView 
    for (i in 0 until menuView.childCount) { 
     val itemView = menuView.getChildAt(i) as BottomNavigationItemView 
     itemView.setShiftingMode(false) 
     itemView.setChecked(false) 
    } 
} 
संबंधित मुद्दे