2017-04-19 3 views
-3

Ich versuche, Aktionsleiste in meiner App zu implementieren. Ich verwende getSupportedActionBar() und eine Menü-Datei, die zwei Symbole und einen Text enthält. Ich möchte das erste Symbol auf der linken Seite, den Titel in der Mitte und das zweite Symbol auf der rechten Seite zeigen, aber ich kann es nicht tun.Wie implementiere ich eine Aktionsleiste in Android?

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" 
    tools:context="yieldcurve.jamakharch.HomeActivity" 
    > 
    <item 
     android:id="@+id/Id_logo_notification" 
     android:title="@string/app_name" 
     android:icon="@mipmap/ic_menu_notification" 
     app:showAsAction="always" 
     android:visible="true" 
     android:orderInCategory="0" 
     /> 
    <item 
     android:id="@+id/title" 
     android:title="JMK" 
     app:showAsAction="always" 
     android:visible="true" 
     android:orderInCategory="300"/> 

    <item android:id="@+id/Id_logo_menu" 
     android:title="Menu" 
     android:icon="@mipmap/ic_menu_notification" 
     app:showAsAction="always" 
     android:visible="true" 
     android:orderInCategory="9999"/> 
</menu> 
+1

Gehen Sie für Toolbar anstelle von ActionBar, suchen Sie nach Symbolleisten, finden Sie viele Beispiele. – Bhavnik

+0

Erstellen Sie eine Symbolleiste anpassen –

+0

Google für einige Tutorials. Dies ist nicht der richtige Ort, um nach Code- oder Coding-Lektionen zu suchen. –

Antwort

0

toolbar.xml

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

     <TextView 
      android:layout_toStartOf="@+id/home_icon" 
      android:padding="2dp" 
      android:textColor="@color/color_dark_blue" 
      android:layout_width="190dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start|center" 
      android:id="@+id/toolbar_title" 
      android:maxLines="1"/> 

     <ImageView 
      android:layout_gravity="end|center" 
      android:id="@+id/cart_imageview" 
      android:src="@drawable/ic_tool_cart" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@null" /> 

     <ImageView 
      android:contentDescription="@null" 
      android:layout_gravity="end|center" 
      android:id="@+id/search_tool" 
      android:src="@drawable/ic_tool_search" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

Und Fügen Sie in Ihrem Layout wie main_activity.xml

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

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

    <ListView 
     android:id="@+id/cart_ListView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

In der Klasse

declar e global

private Toolbar mToolbar; 

in OnCreate Methode

mToolbar = (Toolbar) findViewById(R.id.back_toolbar); 

enter image description here

0

Insted Ihrer Menü Datei aufblasen aufblasen nur auf Symbolleiste, um Ihre benutzerdefinierte Ansicht.

Versuchen Code unten aus

<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> 

innerhalb RelativeLayout Ihrer Ansicht hinzufügen.

0
<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="?actionBarSize" 
    android:background="@color/colorAccent" 
    app:contentInsetEnd="0dp" 
    app:contentInsetStart="0dp" 
    tools:context="com.agileinfoways.toolbardemo.MainActivity"> 

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:src="@mipmap/ic_launcher_round" /> 

     <android.support.v7.widget.AppCompatTextView 
      style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_centerVertical="true" 
      android:text="Title" 
      android:textColor="#ffffff" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@mipmap/ic_launcher_round" /> 

    </RelativeLayout> 

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

Die MenuInflator Klasse ermöglicht es Aktionen in einer XML-Datei definiert aufzublasen und fügt sie in der Aktionsleiste. Der MenuInflator kann über die Methode getMenuInflator() von Ihrer Aktivität aus aufgerufen werden.

@Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
       MenuInflater inflater = getMenuInflater(); 
       inflater.inflate(R.menu.mainmenu, menu); 
       return true; 
     } 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.Id_logo_notification: 
      Toast.makeText(this, "Id_logo_notification selected", Toast.LENGTH_SHORT) 
      .show(); 
      break; 
    case R.id.title: 
      Toast.makeText(this, "title selected", Toast.LENGTH_SHORT) 
      .show(); 
      break; 
    case R.id.Id_logo_menu: 
      Toast.makeText(this, "Id_logo_men selected", Toast.LENGTH_SHORT) 
      .show(); 
      break; 
    default: 
      break; 
    } 

    return true; 
} 
Verwandte Themen