Action Bar Tabs at the bottom?

后端 未结 2 432
走了就别回头了
走了就别回头了 2021-01-06 04:13

I\'m trying to place the action bar tabs at the bottom of my app, but I need some help with it (if it\'s even possible).

The code:

actionBar.setNavig         


        
相关标签:
2条回答
  • 2021-01-06 04:39

    It kind of goes against the Android guidelines to put tabs on the bottom, but a split action bar should be able to accomplish what you're trying to do.

    0 讨论(0)
  • 2021-01-06 04:43

    There is another way to this but you have to 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 have to place 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;
        }
    }

    I hope it would help you.

    0 讨论(0)
提交回复
热议问题