2014-01-07 23 views
11

Ich bin gerade mit der Android-Entwicklung aus IOS und wieder auf ein Problem stubled. Und nach 1 Tag des Versuchs entschied ich, dass ich die Leute des Stapelüberlaufs fragen werde.Android ActionBar benutzerdefinierte Layout-Styling

Also mein Problem:

Ich habe eine App und in der Aktionsleiste (standardmäßig keine sherlock) Ich möchte ein Logo und 2 Textzeilen. Und durch das Internet fount ich die setCustomView für die Aktionsleiste. Und das benutzerdefinierte XML wird geladen, aber ich kann das Layout nicht richtig bekommen.

Also zuerst i-Setup die Aktionsleiste:

private void setupActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayUseLogoEnabled(false); 
    actionBar.setDisplayHomeAsUpEnabled(false); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(false); 

    LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // layout which contains your button. 

    actionBar.setCustomView(customNav, lp1); 
} 

als der 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:layout_gravity="fill_horizontal" 
    android:orientation="horizontal" 
    android:background="@color/Blue"> 

    <TextView 
     android:id="@+id/text_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/title_menu" 
     android:textColor="@color/White" 
     android:paddingLeft="5dp" 
     android:layout_gravity="left"/> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/Icon" 
     android:layout_gravity="center"/> 

    <TextView 
     android:id="@+id/text_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/title_help" 
     android:layout_gravity="right" 
     android:textColor="@color/White" 
     android:paddingRight="5dp"/> 

</LinearLayout> 

Aber das Ergebnis ist etwas, was ich nicht bin auf der Suche nach:

Problem in an image

Also was vergesse ich/mache ich falsch/oder sollte ich tun?

Antwort

13

versuchen, das Layout Wurzel in relatives Layout ändern

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="fill_horizontal" 
android:orientation="horizontal"> 

<TextView 
    android:text="left text" 
    android:layout_toLeftOf="@+id/icon" 
    android:layout_alignParentLeft="true" 
    android:id="@+id/text_left" 
    android:gravity="left" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="5dp"/> 

<ImageView 
    android:layout_centerHorizontal="true" 
    android:id="@+id/icon" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:src="@drawable/bg_sunrise" 
    android:layout_gravity="center"/> 

<TextView 
    android:text="Right txt" 
    android:layout_toRightOf="@+id/icon" 
    android:id="@+id/text_right" 
    android:layout_width="match_parent" 
    android:gravity="right" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:paddingRight="5dp"/> </RelativeLayout> 
+0

Ich liebe dich, ihre getan i taillierte 1 Tag. Und jetzt kann ich weitermachen. Danke – Msmit1993

+0

du bist willkommen ^^ –

+1

Danke das war nützlich. Wenn Sie ein anderes Layout benötigen, verwenden Sie immer ein RelativeLayout als root! – Gibberish

2

Sie nicht Ihr Ergebnis zu erzielen ist, weil:

enter image description here

u müssen:

  1. wenn Sie AppCompat Thema verwenden

enter image description here

enter image description here

auf Ihre Aktivität Layout hinzufügen
  1. in Code zB. Setzen Sie in onCreate() eine Symbolleiste, um die Aktionsleiste zu ersetzen.

enter image description here