2016-04-28 6 views
13

enter image description hereखोज दृश्य की खोज आइकन को दाईं तरफ कैसे स्थानांतरित करें?

यहाँ android.support.v7.widget.SearchView उपयोग करते हुए मेरे लेआउट है। मैं के बाद मैं आइकन पर क्लिक करें सही पक्ष के लिए खोज आइकन ले जाना चाहते हैं, लेकिन मैं किसी भी विधि नहीं मिली है ... `

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways|snap" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <android.support.v7.widget.SearchView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </android.support.v7.widget.Toolbar> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_main_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</android.support.design.widget.AppBarLayout> 

`

, यह इस लेआउट के लिए बदल जाएगा enter image description here

उत्तर

15

आप इसे प्रोग्राम के रूप में

SearchView search = (SearchView) item.getActionView(); 
    search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT)); 
+0

thx! यह पूरी तरह से काम करता है, और यह मेरे शीर्षक को कवर नहीं करेगा! – meunicorn

7
<android.support.design.widget.AppBarLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:theme="@style/AppTheme.AppBarOverlay"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways|snap" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <RelativeLayout 
     android:id="@+id/not" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
         > 
    <android.support.v7.widget.SearchView 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 
</android.support.v7.widget.Toolbar> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_main_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</android.support.design.widget.AppBarLayout> 
+0

धन्यवाद! ये अच्छी तरह काम करता है ! – meunicorn

1

प्रयास करें इस ... यह रूप में आप SearchView टैग के अंदर इस लाइन को जोड़ने के expect.Just लग रहा है।

एंड्रॉयड: गुरुत्वाकर्षण = "right"

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways|snap" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <android.support.v7.widget.SearchView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right" /> 
</android.support.v7.widget.Toolbar> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_main_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

+3

आपके उत्तर के लिए धन्यवाद, लेकिन यह काम नहीं करता – meunicorn

+0

अपना पूरा कोड पोस्ट करें। क्योंकि मैंने आपके स्निपेट का परीक्षण किया है। यह काम किया गया था। –

9

आप की तरह menu.xml में खोज कार्रवाई जोड़ सकते हैं कर सकते हैं नीचे:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools"> 
    <item 
     android:id="@+id/action_search" 
     android:icon="@mipmap/ic_search_white_24dp" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="ifRoom|collapseActionView" /> 
    <item android:id="@+id/action_download" 
     android:title="@string/menu_action_download" 
     android:icon="@mipmap/ic_file_download_white_24dp" 
     android:orderInCategory="100" 
     app:showAsAction="always" /> 
</menu> 

Normal look Collapsed look

0

आप किसी भी अन्य दृश्य में ActionBar की SearchView बाहर का उपयोग करना चाहते मामले में आप दृष्टिकोण this साइट पर पोस्ट उपयोग कर सकते हैं (उदाहरण के ConstraintLayout के लिए मान लीजिए)। मूल रूप से आइकन ImageView लेआउट की शुरुआत में स्थित है (या मैं कह सकता हूं "यह अपने मूल" में जोड़ा गया पहला तत्व है)। इस कोड के साथ, आप इसके लिए खोज रहे हैं, फिर इसे माता-पिता से हटा दें, और आखिरकार इसे वापस रखें। परिणाम: इसके बारे में SearchView विजेट ... समस्या हल "अंत" पर तैनात किया जाएगा ...

//Get ImageView of icon 
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon); 

//Get parent of gathered icon 
ViewGroup linearLayoutSearchView = (ViewGroup) searchViewIcon.getParent(); 
//Remove it from the left... 
linearLayoutSearchView.removeView(searchViewIcon); 
//then put it back (to the right by default) 
linearLayoutSearchView.addView(searchViewIcon); 

परीक्षण किया गया, काम करने, संतुष्ट ...;)

0

करने के लिए SearchView गुरुत्वाकर्षण की स्थापना एक्सएमएल या प्रोग्रामेटिक रूप से काम करता है लेकिन जब SearchView का विस्तार किया गया था तो यह दाईं ओर चौड़ाई का एक छोटा सा क्षेत्र ले रहा था जो कि मेरे मन में इच्छित रूप से नहीं था, मैं चाहता था कि यह पूरी चौड़ाई ले।

नोट 1: इस समाधान ने शीर्षक को दिखाने की अनुमति नहीं दी, इसलिए मैंने शीर्षक रखने के लिए TextView जोड़ा।

नोट 2: जब SearchView विस्तार किया गया था, यह कस्टम शीर्षक पर ओवरलैप होता तो मैं

यहाँ SearchView की विस्तृत/संक्षिप्त करें साथ टूलबार के रूप में SearchView पृष्ठभूमि बनाया छुपा रखने से बचने के लिए/शीर्षक दिखा Toolbar कोड है:

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize"> 

      <TextView 
       android:id="@+id/toolbar_title" 
       style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:gravity="center_vertical" 
       android:text="Toolbar Title" 
       android:textColor="@android:color/white" /> 

      <android.support.v7.widget.SearchView 
       android:id="@+id/search_view" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:background="?attr/colorPrimary" 
       android:gravity="right" /> 
     </RelativeLayout> 
    </android.support.v7.widget.Toolbar> 

गतिविधि के अंदर, मैं बाईं ओर SearchView संरेखित करने के लिए बंद और SearchClick श्रोताओं का इस्तेमाल किया।

mSearchView = findViewById(R.id.search_view); 

    mSearchView.setOnCloseListener(new SearchView.OnCloseListener() { 
     @Override 
     public boolean onClose() { 
      RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams(); 
      param.removeRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      //set layout params to cause layout update 
      mSearchView.setLayoutParams(param); 
      return false; 
     } 
    }); 
    mSearchView.setOnSearchClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams(); 
      param.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      //set layout params to cause layout update 
      mSearchView.setLayoutParams(param); 
     } 
    }); 
संबंधित मुद्दे