2017-05-08 52 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