Как настроить вкладки Android или изменение фона?

Я очень новичок в андроиде. Теперь я создаю небольшое приложение.

Мне нужно изменить внешний вид поиска по умолчанию для Android в версии 2.2. Итак, здесь я пытаюсь изменить фон вкладок. Можете ли вы помочь мне сделать это.

Мне нравится способ использования xml/style.

Это то, что мне нужно, что фактический результат.

Вкладки


person Sibiraj PR    schedule 27.07.2013    source источник
comment
stackoverflow.com/questions/2099959/   -  person selva_pollachi    schedule 27.07.2013
comment
Я попробовал реальную ссылку, предоставленную сельвой. Но не получить результат. Может быть, это моя проблема.   -  person Sibiraj PR    schedule 27.07.2013
comment
@SibirajPR, посмотрите здесь, joshclemm.com/blog/?p=136   -  person yugidroid    schedule 27.07.2013


Ответы (1)


XML-файл вашего хоста вкладок

TabHost

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <LinearLayout android:orientation="vertical" 
        android:layout_width="fill_parent" android:layout_height="fill_parent"> 

        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" android:layout_height="fill_parent"> 
        </FrameLayout> 

    </LinearLayout> 

</TabHost> 

в вашем основном действии

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    setupTab(new TextView(this), "Tab 1");
    setupTab(new TextView(this), "Tab 2");
    setupTab(new TextView(this), "Tab 3");
}

private void setupTab(final View view, final String tag) {
    View tabview = createTabView(mTabHost.getContext(), tag);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
        public View createTabContent(String tag) {
            return view;
        }
    });
    mTabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

CustomTabsLayout tabs_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
    android:padding="10dip" android:gravity="center" android:orientation="vertical">

    <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Title"
        android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>

tab_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#f8f8f8" />
</selector>

tab_bg_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@android:color/transparent" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

tab_bg_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
        android:endColor="#696969" android:angle="-90" />
</shape>

tab_bg_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#5C5C5C" android:centerColor="#424242"
    android:endColor="#222222" android:angle="-90" />
</shape>

И, наконец, в вашем основном классе занятий

mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

И закончить :)

person Abdulkadir NURKALEM    schedule 27.07.2013
comment
Спасибо за этот длинный ответ. Я свяжусь с вами после того, как реализую его. - person Sibiraj PR; 27.07.2013
comment
Это помощь вам? :) - person Abdulkadir NURKALEM; 04.10.2013
comment
@AbdulkadirNURKALEM, не могли бы вы рассказать мне, что такое tab_divider в mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); - person Hassaan Rabbani; 13.12.2013
comment
@SibirajPR, не могли бы вы рассказать мне, что такое tab_divider в mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); - person Hassaan Rabbani; 13.12.2013
comment
tabhost дает мне исключение nullpointerexception .. Это работает для кого-нибудь? - person Dusean Singh; 30.01.2014
comment
@AbdulkadirNURKALEM Что здесь tab_devider? - person Shabbir Dhangot; 12.04.2014
comment
получение ошибки в mTabHost.addTab(setContent); кто-нибудь знает, как получить null pointer exception - person Kartheek s; 29.05.2014
comment
Ответ на вопрос Картика = Добавить mTabHost.setup(); к методу onCreate. Вот так: @Override public void onCreate(Bundle saveInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); setupTab (новый TextView (это), вкладка 1); setupTab (новый TextView (это), вкладка 2); setupTab (новый TextView (это), вкладка 3); } - person Gene; 14.08.2014
comment
Большое спасибо .... Но отсутствует одна строка кода, которая вызывает исключение NullPointerException, т. Е. После этой строки в MainActivity mTabHost = (TabHost) findViewById (android.R.id.tabhost); ......напишите mTabHost.setup();....Всего хорошего - person pioneerBhawna; 22.09.2014
comment
где R.drawable.tab_divider xml? - person Shihab Uddin; 19.12.2015