2017-05-08 58 views
0

试图将TextViews推送到ViewPager容器的底部。现在TextView仅保留在顶部,当我wrap_contentmatch_parent。这里的XML:viewpager儿童的重力底部

<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> 
+0

你能附上你所得到的屏幕,您从视图 – Avi

+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 

您还需要一个适配器添加views--

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(); 
} 


} 
+0

最终找到非常相似的解决方案,但方法与您的方法相同。谢谢! – Ryan