2

Ich habe benutzerdefinierte Symbolleiste mit Such-und Einstellungssymbol gemacht. wenn ich auf das Suchsymbol klicke, sollte edittext mit match_parent übereinstimmen und den verfügbaren Platz bis zum Suchsymbol belegen. Es passiert jedoch nicht. Es erscheint nur mit sehr wenig Platz auf der oberen linken Seite. Bitte helfen Sie, wo ich bin falschmatch_parent funktioniert nicht in benutzerdefinierten Symbolleiste

enter image description here

ich mit xml meinen ganzen Code teilen möchten

main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#DCDCDC" 
android:padding="0dp" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="myresolver.faisal.home.com.myresolver.MainActivity"> 

<include 
    android:id="@+id/toolbar" 
    layout="@layout/my_toolbar"></include> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar" 
    android:fillViewport="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:padding="1dp" 
     android:weightSum="4" 

     > 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96"></ImageView> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 

      <ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" /> 
     </LinearLayout> 


    </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

mytoolbar.xml

<?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:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/orange" app:titleTextColor="@color/white"></android.support.v7.widget.Toolbar>

mein main_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<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 
    app:showAsAction="always" 
    android:title="@string/search" 
    android:icon="@drawable/ic_search_white_24dp" 
    android:id="@+id/search" 
    ></item> 
<item 
    app:showAsAction="always" 
    android:title="@string/setting" 
    android:icon="@drawable/ic_settings_white_24dp" 
    android:id="@+id/setting"></item> 
</menu> 

mein search_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal"> 

<EditText 
    android:id="@+id/searchEditText" 
    android:layout_width="match_parent" // THIS IS NOT WORKING 
    android:layout_height="wrap_content" 
    android:imeOptions="actionSearch" 
    android:inputType="text" /> 

</LinearLayout> 

Meine Tätigkeit

EditText searchEditText; 
boolean isSearchBoxOpen = false; 
Toolbar toolbar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

} 

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 


    return super.onPrepareOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
     case R.id.search: 
      onSearchedHandler(); 
      break; 

     case R.id.setting: 
      Toast.makeText(this, "Setting is UnderConstruction", Toast.LENGTH_LONG).show(); 
      break; 
    } 


    return super.onOptionsItemSelected(item); 
} 

public void onSearchedHandler() { 
    if (isSearchBoxOpen) { 

     String word = searchEditText.getText().toString(); 

     if (word != null) { 
      Toast.makeText(this, "Searching for ..." + word, Toast.LENGTH_LONG).show(); 
     } 
    } else { 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayShowCustomEnabled(true); 
     actionBar.setCustomView(R.layout.search_layout); 
     actionBar.setDisplayShowTitleEnabled(false); 

     searchEditText = (EditText) actionBar.getCustomView().findViewById(R.id.searchEditText); 

     searchEditText.requestFocus(); 
     InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
     keyboard.showSoftInput(searchEditText, InputMethodManager.HIDE_IMPLICIT_ONLY); 


     isSearchBoxOpen = true; 

    } 

} 

@Override 
public void onBackPressed() { 

    if (isSearchBoxOpen) { 

     ActionBar actionBar = getSupportActionBar(); 

     actionBar.setDisplayShowCustomEnabled(false); 
     actionBar.setDisplayShowTitleEnabled(true); 
     isSearchBoxOpen = false; 

     return; 

    } 

    super.onBackPressed(); 
} 
+0

lineare Layoutbreite bei search_layout sollte einpacken Inhalt – Rasel

+0

Ich nicht, was verstanden werden, sagen Sie? – FaisalAhmed

+0

search_layout.xml \t -> lineare Layout Höhe – Rasel

Antwort

2

Wenn die benutzerdefinierte Ansicht als Ergebnis des Aufrufs setCustomView (Resid) aufgeblasen wird, bewegen , wrap_content wird erzwungen (wenn Sie in den View-Dump schauen, können Sie sehen, dass das Wrapping LinearLayout geschrumpft ist, nicht der EditText). Sie können die benutzerdefinierte Ansicht auf folgende Weise einstellen:

View customView = LayoutInflater.from(this).inflate(R.layout.search_layout, null); 
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); 
    actionBar.setCustomView(customView, params); 
+0

Vielen Dank Bruder. Ich habe es seit zwei Tagen gesucht. endlich funktioniert es gut. Du hast es wirklich gut erklärt, was das eigentliche Problem war. Ich habe verzweifelt danach gesucht. Und es erhöht mein Wissen. Danke noch einmal – FaisalAhmed

6

Verwenden Sie den folgenden Code für die Symbolleiste xlm Layout:

<android.support.v7.widget.Toolbar 
    xmlns:app="schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/primaryColor" 
    android:contentInsetLeft="0dp" 
    android:contentInsetStart="0dp" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    android:contentInsetRight="0dp" 
    android:contentInsetEnd="0dp" 
    app:contentInsetRight="0dp" 
    app:contentInsetEnd="0dp" /> 
+0

dies nicht funktioniert – FaisalAhmed

+0

Diese +1 für mich gearbeitet wird. – Shailesh

+0

Danke! Gut für mich! –

0

Die Polsterung in der RelativeLayout in main_activity.xml i s es nicht daran, den vollen Raum so anstatt dass verhindert

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#DCDCDC" 
android:padding="0dp" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="myresolver.faisal.home.com.myresolver.MainActivity"> 

Sie die Polsterungen an den Scroll so dass

<ScrollView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_below="@+id/toolbar" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:fillViewport="true"> 
+0

das funktioniert auch nicht .. – FaisalAhmed

+0

@FaisalAhmed was gibt es dir? – Seaskyways

+0

blättern Ansicht mit der Auffüllung von allen Seiten. Und edittext wie es war. Keine Änderung – FaisalAhmed

Verwandte Themen