The "Action" tab below?

I am trying to place the action bar tabs at the bottom of my application, but I need help (if possible).

Code:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab tabA = actionBar.newTab().setText("Games"); ActionBar.Tab tabB = actionBar.newTab().setText("Apps"); ActionBar.Tab tabC = actionBar.newTab().setText("Themes"); tabA.setTabListener(new ActionBarTabListener()); tabB.setTabListener(new ActionBarTabListener()); tabC.setTabListener(new ActionBarTabListener()); actionBar.addTab(tabA); actionBar.addTab(tabB); actionBar.addTab(tabC); 
+4
source share
2 answers

This contradicts Android's recommendations to put tabs at the bottom, but the split action bar should be able to accomplish what re trying to do.

+1
source

There is another way, but you should use FrgamentTabHost.

Here is the code. layout / activity _main.xml

 enter <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom"/> </LinearLayout> 

You should put the TabWidget below the FrameLayout.

Layout / fragment_layout.xml

 enter code here<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:background="#eaecee"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceMedium" /> 

MainActivity.java

 import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.addTab( mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null), FragmentTab.class, null); mTabHost.addTab( mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null), FragmentTab.class, null); mTabHost.addTab( mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null), FragmentTab.class, null); } } 

FragmentTab.java

 import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentTab extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_layout, container, false); TextView tv = (TextView) v.findViewById(R.id.text); tv.setText(this.getTag() + " Content"); return v; } } 

Hope this helps you.

0
source

Source: https://habr.com/ru/post/1440033/


All Articles