Why the tab widget is above the content in android?

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

Currently I work on a tabhost layout.

In most android app the layout is:

tab1 | tab 2 ____________  Tab 1 content  (if I press on tab1) 

However, what I would like to achieve is

Tab 1 content  (if I press on tab1) ____________ tab1 | tab 2 

Here is the main xml (backbone of app) :

<LinearLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <FrameLayout         android:id="@android:id/tabcontent"         android:layout_width="0dp"         android:layout_height="0dp"         android:layout_weight="0" />      <FrameLayout         android:id="@+id/realtabcontent"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1" />      <TabWidget         android:id="@android:id/tabs"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="0"         android:orientation="horizontal" /> </LinearLayout> 

The tab content layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#31152C"     android:gravity="center"     tools:context=".MonitoringActivity" >      <ScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <LinearLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical" >              <ImageView                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:adjustViewBounds="true"                 android:src="@drawable/home" />         </LinearLayout>     </ScrollView>  </LinearLayout> 

The main class:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);          setContentView(R.layout.main);      ctx = this;      gs = (MyApp) getApplication();      tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);     tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);      tabHost.addTab(tabHost.newTabSpec("main").setIndicator(""),Home.class, null);     tabHost.addTab(tabHost.newTabSpec("carpark").setIndicator("",getResources().getDrawable(R.drawable.btn_park)), CarPark.class,null);     tabHost.addTab(tabHost.newTabSpec("shop").setIndicator("",getResources().getDrawable(R.drawable.btn_shop)), Shop.class,null);     tabHost.getTabWidget().setDividerDrawable(null);       tabHost.setCurrentTab(0); } 

Thanks for helping

回答1:

you can add tabwidget to the bottom using the given code..

<?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="match_parent"     android:layout_height="match_parent" >      <RelativeLayout         android:layout_width="match_parent"         android:layout_height="match_parent" >          <FrameLayout             android:id="@android:id/tabcontent"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_above="@android:id/tabs" />          <TabWidget             android:id="@android:id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_alignParentBottom="true" />     </RelativeLayout>  </TabHost> 


回答2:

Found out the solution using the support fragment tab host:

    <?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" >      <FrameLayout         android:id="@+id/realtabcontent"         android:layout_width="match_parent"         android:layout_height="0dip"         android:layout_weight="1" />      <android.support.v4.app.FragmentTabHost         android:id="@android:id/tabhost"         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <TabWidget             android:id="@android:id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="horizontal" />     </android.support.v4.app.FragmentTabHost>  </LinearLayout> 


回答3:

Try this way,hope this will help you to solve your problem.

activity_main.xml

<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:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent">          <FrameLayout             android:id="@android:id/tabcontent"             android:layout_width="0dp"             android:layout_height="0dp"             android:layout_weight="0"/>          <FrameLayout             android:id="@+id/realtabcontent"             android:layout_width="match_parent"             android:layout_height="0dp"             android:layout_weight="1"/>          <TabWidget             android:id="@android:id/tabs"             android:orientation="horizontal"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="0"/>      </LinearLayout> </android.support.v4.app.FragmentTabHost> 

tab1_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#31152C"     android:gravity="center">      <ScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <LinearLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical"             android:gravity="center">              <ImageView                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:adjustViewBounds="true"                 android:src="@drawable/ic_launcher" />              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginTop="10dp"                 android:textSize="20sp"                 android:text="Home"/>         </LinearLayout>     </ScrollView>  </LinearLayout> 

tab2_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#31152C"     android:gravity="center">      <ScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <LinearLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical"             android:gravity="center">              <ImageView                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:adjustViewBounds="true"                 android:src="@drawable/ic_launcher" />              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginTop="10dp"                 android:textSize="20sp"                 android:text="Car Park"/>         </LinearLayout>     </ScrollView>  </LinearLayout> 

tab3_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#31152C"     android:gravity="center">      <ScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <LinearLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical"             android:gravity="center">              <ImageView                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:adjustViewBounds="true"                 android:src="@drawable/ic_launcher" />              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginTop="10dp"                 android:textSize="20sp"                 android:text="Shop"/>         </LinearLayout>     </ScrollView>  </LinearLayout> 

MainActivity.java

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(), R.id.realtabcontent);          mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1",getResources().getDrawable(R.drawable.ic_launcher)),                 Home.class, null);         mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2",getResources().getDrawable(R.drawable.ic_launcher)),                 CarPark.class, null);         mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3", getResources().getDrawable(R.drawable.ic_launcher)),                 Shop.class, null);     } } 

Home.java

public class Home extends Fragment{     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {         View V = inflater.inflate(R.layout.tab1_view, container, false);         return V;     } } 

CarPark.java

public class CarPark extends Fragment {     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {         View V = inflater.inflate(R.layout.tab2_view, container, false);         return V;     } } 

Shop.java

public class Shop extends Fragment {     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {         View V = inflater.inflate(R.layout.tab3_view, container, false);         return V;     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!