viewpager儿童的重力底部

时间:2017-05-08 02:19:15

标签: android xml layout

尝试将TextView推送到ViewPager容器的底部。现在,TextViews仅在wrap_contentmatch_parent时保持在顶部。这是XML:

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_above="@+id/layout_view_pager_navigation">

        <TextView
            android:id="@+id/string_main_intro_1"
            android:text="@string/string_main_intro_1"
            android:background="@color/buttonColor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/textPrimary"
            android:textSize="22sp"
            android:textAlignment="center"
            android:gravity="bottom"/>
        <TextView
            android:id="@+id/string_main_intro_2"
            android:text="@string/string_main_intro_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/textPrimary"
            android:textSize="22sp"
            android:textAlignment="center"
            android:gravity="bottom"/>
        <TextView
            android:id="@+id/string_main_intro_3"
            android:text="@string/string_main_intro_3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/textPrimary"
            android:textSize="18sp"
            android:textAlignment="center"
            android:gravity="bottom"/>
        <TextView
            android:id="@+id/string_main_intro_4"
            android:text="@string/string_main_intro_4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/textPrimary"
            android:textSize="18sp"
            android:textAlignment="center"
            android:gravity="bottom"/>
    </android.support.v4.view.ViewPager>

1 个答案:

答案 0 :(得分:0)

我建议为TextViews创建一个不同的布局,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:id="@+id/my_tvs"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    >

<TextView
    android:id="@+id/string_main_intro_1"
    android:text="@string/string_main_intro_1"
    android:background="@color/buttonColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/textPrimary"
    android:textSize="22sp"
    android:textAlignment="center"
    />
<TextView
    android:id="@+id/string_main_intro_2"
    android:layout_below="@+id/string_main_intro_1"
    android:text="@string/string_main_intro_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/textPrimary"
    android:textSize="22sp"
    android:textAlignment="center"
    />
<TextView
    android:id="@+id/string_main_intro_3"
    android:layout_below="@+id/string_main_intro_2"
    android:text="@string/string_main_intro_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/textPrimary"
    android:textSize="18sp"
    android:textAlignment="center"
    />
<TextView
    android:id="@+id/string_main_intro_4"
    android:layout_below="@+id/string_main_intro_3"
    android:text="@string/string_main_intro_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/textPrimary"
    android:textSize="18sp"
    android:textAlignment="center"
    />
</RelativeLayout>

然后您的ViewPager布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:layout_above="@+id/layout_view_pager_navigation">

</RelativeLayout>

然后在您的Viewpager.java中添加视图:

ViewPager vpp = (ViewPager) findViewById(R.id.pager);
    this.addPages(vpp);
    RelativeLayout relative = (RelativeLayout) findViewById(R.id.my_tvs);
    relative.setGravity(RelativeLayout.ALIGN_BOTTOM);


//ADD ALL PAGES TO TABLAYOUT
private void addPages(ViewPager pager){
    MyFragPagerAdapter adapter=new 
MyFragPagerAdapter(getSupportFragmentManager());
    adapter.addPage(new MyCustomView()); <----------- This will be the layout with your textViews

您还需要一个适配器来添加视图 -

public class MyFragPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> pages=new ArrayList<>();

public MyFragPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return pages.get(position);
}

@Override
public int getCount() {
    return pages.size();
}

//ADD A PAGE
public void addPage (Fragment f){
    pages.add(f);
}


//SET TITLE FOR TAB
@Override
public CharSequence getPageTitle(int position) {
    return pages.get(position).toString();
}


}