如何在选项卡式活动中将选项卡放置在应用程序的底部 [英] How to place tabs at bottom of app in a tabbedactivity
问题描述
我正在尝试使用android中的tabbedActivity创建一个简单的应用程序. 我想将选项卡放在屏幕底部.
I'm trying to use a tabbedActivity in android to create a simple app. I would like to put my tabs at the bottom of the screen.
这是用android studio创建我的Activity时得到的.
Here is what i've got when creating my Activity with android studio.
我想要的是位于屏幕底部的"SECTION 1 SECTION 2 SECTION 3 SECTION"部分.
What I would like, is the part with "SECTION 1 SECTION 2 SECTION 3" to be at the bottom of the screen.
这是我的xml文件:
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
此代码包装在CoordinatorLayout
中.
问题是,如果我拿TabLayout
并将其放在viewPager之后,则会从屏幕上删除它.
This code is wrapped in a CoordinatorLayout
.
The problem is that if I take the TabLayout
and put it after the viewPager, it is erased from the screen.
请问有人知道吗?
感谢@Vivek Mishra的回答,它有效!谢谢
EDIT : Thanks to @Vivek Mishra answer, it works ! Thanks
推荐答案
您可以像这样用tabhost在底部设置标签,
You can set tabs at bottom using tabhost like this way,
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc916"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 1" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#da8200"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 2" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 3" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost host = (TabHost) findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab One");
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("Tab Two");
spec.setContent(R.id.tab2);
spec.setIndicator("Tab Two");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("Tab Three");
spec.setContent(R.id.tab3);
spec.setIndicator("Tab Three");
host.addTab(spec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
输出:
这篇关于如何在选项卡式活动中将选项卡放置在应用程序的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!