[英]Static content with Viewpager in Android
I've tried multiple ways of adding a footer and a header in activity_main.xml which has a viewpager inside it. 我尝试了多种方法在activity_main.xml中添加页脚和页眉,该文件中包含viewpager。 So far when I add a header above the viewpager, it's shown in final result but the footer isn't. 到目前为止,当我在viewpager上方添加页眉时,最终结果中将显示该页眉,但页脚未显示。 Here is the xml code 这是XML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg50"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
And here is the activity 这是活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new LoginFragment(), "Login");
adapter.addFragment(new RegisterFragment(), "Register");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
} }
sreenshot of the result So the question is ,how to display content below the view pager. 结果的快照因此,问题是,如何在视图分页器下方显示内容。
Change your viewpager layout height to wrap_content. 将您的viewpager布局高度更改为wrap_content。
Or just change your root layout to <RelativeLayout>
and set add this tag to your view pager. 或者只是将您的根布局更改为<RelativeLayout>
并将其设置为将此标签添加到视图分页器中。
Like so: 像这样:
<RelativeLayout>
...
<ViewPager
...
android:layout_height="match_parent"
android:layout_above="@id/your_footer_id"/>
...
</RelativeLayout>
Set to your viewpager: 设置为您的viewpager:
android:layout_height="0dp"
android:layout_weight="1"
This will make it fill all available space in LinearLayout after other children are laid out. 在布置其他子项之后,这将使其填充LinearLayout中的所有可用空间。
If you want a static footer below ViewPager
you can use layout below. 如果要在ViewPager
下使用静态页脚, ViewPager
可以使用以下布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:background=""
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer"
android:layout_below="@+id/tabs" />
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#FF0000"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.