2012-04-16 20 views
10

Die App verfügt über eine ActionBar und die ActionBar hat Navigationsregisterkarten, die jeweils Text enthalten. Ich muss neben dem Text Symbole zu den Tabs hinzufügen.Ausrichten der Registerkarten der ActionBar-Navigation über dem Tab-Text?

Ich habe die Symbole mit ActionBar.Tab.setIcon(drawable) erfolgreich hinzugefügt, aber das Symbol wird auf der linken Seite des Tabs angezeigt.

Wie verschiebe ich das Tab-Symbol, so dass es vor der Navigation Registerkarte Text ist?

(Ich verwende ActionBarSherlock, aber Abbildung der Lösung dieses Problems wird in nativen und ABS-Implementierungen arbeiten)

Antwort

12

Sie können für jede Registerkarte eine benutzerdefinierte Ansicht festlegen.

https://gist.github.com/3167287

PropositionListActivity.java

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.Tab; 
import com.actionbarsherlock.app.SherlockActivity; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Color; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.ImageView; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.app.NavUtils; 

public class PropositionListActivity extends SherlockActivity implements ActionBar.TabListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.proposition_list); 

     getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     createTab(R.layout.tab_to_opine, R.id.tab_to_opine_title, "Vou Opinar"); 
     createTab(R.layout.tab_opined, R.id.tab_opined_title, "Já Opinei"); 
     createTab(R.layout.tab_feedback, R.id.tab_feedback_title, "Feedback"); 
    } 

    public void createTab(int view, int titleView, String title) { 
     ActionBar.Tab tab = getSupportActionBar().newTab(); 
     tab.setTabListener(this); 
     tab.setCustomView(view); 
     getSupportActionBar().addTab(tab); 

     ((TextView) findViewById(titleView)).setText(title); 
    } 
} 

tab_to_opine.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="vertical" > 

    <ImageView 
     android:id="@+id/tab_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/tab_to_opine" 
     android:background="@android:color/transparent" /> 

    <TextView 
     android:id="@+id/tab_to_opine_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+1

Es wäre schön, wenn Sie den Code hier, falls die Verbindung bricht gebucht. – Joony

+1

Fertig, Joony. Stack Overflow ist kein perfekter Platzhalter für mehr als ein paar Zeilen Code, also habe ich den Link beibehalten. –

+1

Hier ist ein Link zu vogella.com, wo er zeigt, wie man einer Actionleiste ohne actionBarSherlock eine benutzerdefinierte Ansicht hinzufügt. http://www.vogella.com/articles/AndroidActionBar/article.html#menu_customviews –

1

In der styles.xml Datei können Sie ein Add <item name="android:drawableTop">@drawable/your_image.</item> Das funktioniert, wenn Sie alle wollen die Bilder sind gleich oben. Ich weiß nicht genau, wie man das für Tabs macht, die leider noch andere Bilder verwenden.

0

seine ganz einfach nur android hinzufügen: drawableTop = "@ ziehbar/your_image" auf Ihre Textansicht und entfernen Sie das ImageView

<?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="vertical" >

<TextView 
    android:id="@+id/tab_to_opine_title" 
    android:layout_width="wrap_content" 
    android:drawableTop="@drawable/your_image" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 
Verwandte Themen