2016-03-29 12 views
0

Ich bin nicht in der Lage, Element in Aktionsleiste auszurichten.Kann jemand bitte helfen Sie mir mit dem Problem.Ich möchte Bild an der linken Ecke und Titel in der Mitte und eine Einstellungsoption in der rechten Ecke ausrichten.Es gibt einen Pfeil um auf andere Aktivität zurückzugehen. Ich möchte diese Option entfernen. Bitte beziehen Sie sich auf den Screenshot für weitere Informationen.Wie positioniere ich das Element in der Aktionsleiste?

ActionBar.xml

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

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/pic13"/> 

    <TextView 
     android:id="@+id/actionbar_textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:maxLines="1" 
     android:clickable="false" 
     android:focusable="false" 
     android:longClickable="false" 
     android:textStyle="bold" 
     android:textSize="18sp" 
     android:textColor="#FFFFFF" /> 

    </LinearLayout> 

Aktivität:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_student_profile); 
    final ActionBar abar = getSupportActionBar(); 
    abar.setBackgroundDrawable(getResources().getDrawable(R.color.title));//line under the action bar 
    View viewActionBar = getLayoutInflater().inflate(R.layout.abs_layout, null); 
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar ! 
      ActionBar.LayoutParams.WRAP_CONTENT, 
      ActionBar.LayoutParams.MATCH_PARENT, 
      Gravity.CENTER); 
    TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview); 
    textviewTitle.setText("Test"); 
    abar.setCustomView(viewActionBar, params); 
    abar.setDisplayShowCustomEnabled(true); 
    abar.setDisplayShowTitleEnabled(false); 
    abar.setDisplayHomeAsUpEnabled(true); 
    abar.setIcon(R.drawable.pic13); 
    abar.setHomeButtonEnabled(true); 
} 

enter image description here

+0

statt android: layout_gravity Verwendung android: Schwere –

+0

@helldawg Danke für die Antwort .Aber es ist nicht working.I den Pfeil entfernen möchten, und legen Sie das Bild an diesem Ort. –

+0

Der Pfeil wird angezeigt, weil Sie haben 'abar.setHomeButtonEnabled (true);' –

Antwort

2

diesen Code auf Ihre xml hinzufügen

Toolbar ist ein Viewgroup, können Sie anpassen, so viel wie du willst.

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


    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Title" 
     android:textColor="@android:color/white" /> 
</android.support.v7.widget.Toolbar> 
+0

Entschuldigung .das funktioniert nicht für mich –

+0

Entschuldigung. Ich hatte dies falsch benutzt. Es funktioniert gut und es ist sehr einfach anzupassen. –

+0

Vielen Dank. –

Verwandte Themen