Подтвердить что ты не робот

Выровнять значки навигации ActionBar над текстом вкладки?

В приложении есть ActionBar, а в ActionBar есть вкладки навигации, каждый из которых содержит текст. Мне нужно добавить значки к вкладкам в дополнение к тексту.

Я добавил значки с помощью ActionBar.Tab.setIcon(drawable) успешно, но значок отображается слева от текста вкладки.

Как переместить значок вкладки так, чтобы она была выше текста вкладки навигации?

(Я использую ActionBarSherlock, но думаю, что решение для этого будет работать через собственные версии и версии ABS)

4b9b3361

Ответ 1

Вы можете настроить пользовательский вид для каждой вкладки.

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>

Ответ 2

В файле styles.xml вы можете добавить <item name="android:drawableTop">@drawable/your_image.</item> Это будет работать, если вы хотите, чтобы все изображения были одинаковыми в верхней части. Я не совсем знаю, как это сделать для вкладок, которые, к сожалению, используют разные изображения.

Ответ 3

его очень просто добавить addroid: drawableTop = "@drawable/your_image" в текстовое окно и удалить 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>