2014-10-02 163 views
-2

我知道這是一個相當普遍的問題,但是在Google搜索幾個小時後,我找不到問題的答案。CirclePageIndicator屏幕底部

我正在使用PageViewIndicator庫。我想在佈局的底部添加圓圈指標。我正在嘗試這個,但是當應用程序打開時,它立即崩潰。當我交換CirclePageIndicator和ViewPager的地方時,它可以很好地工作,但指標顯示在活動的頂部。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    /> 


<com.viewpagerindicator.CirclePageIndicator 
    android:id="@+id/indicator" 
    android:padding="10dip" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:background="#000000" 

    /> 

    </LinearLayout> 

這就是我如何初始化它在Java中

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_take_test); 

    mAdapter = new FragmentAdapter(getSupportFragmentManager()); 

    mPager = (ViewPager)findViewById(R.id.pager); 
    mPager.setAdapter(mAdapter); 

    mIndicator = (CirclePageIndicator)findViewById(R.id.indicator); 
    mIndicator.setViewPager(mPager); 

} 

我怎樣才能解決這個只用LinearLayout中?

+0

試試這個'com.viewpagerindicator.CirclePageIndicator mIndicator =(com.viewpagerindicator.CirclePageIndicator)findViewById(R.id.indicator);' – 2014-10-02 09:42:04

+0

它不工作,正如我上面提到的,當我改變的地方尋呼機和指示器,它可以很好地工作 – 2014-10-02 09:47:58

+0

當它崩潰時,你會得到什麼錯誤/異常? – 2014-10-02 09:52:01

回答

3

改爲使用RelativeLayout。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    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="match_parent" 
     /> 


    <com.viewpagerindicator.CirclePageIndicator 
     android:id="@+id/indicator" 
     android:padding="10dip" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:background="#000000" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

</RelativeLayout> 

而且因爲你不希望向我們展示完整的活動(看你的字段聲明)我猜你正在做一些錯誤的,因爲沒有辦法,這個代碼將拋出一個轉換異常:

FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager()); 

ViewPager pager = (ViewPager)findViewById(R.id.pager); 
pager.setAdapter(adapter); 

CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator); 
indicator.setViewPager(pager); 
+0

不幸的是它再次崩潰,我在 – 2014-10-02 10:25:41

+0

之前嘗試過同樣的事情然後你將不得不重新構造你的問題。 '但是當應用程序打開時立即崩潰 - 「它完美地工作」。應用程序在打開時如何崩潰,然後完美地工作?還向我們展示了充分的活動,而不僅僅是的onCreate – 2014-10-02 10:28:20

+0

當我換CirclePageIndicator和ViewPager的地方它完美的作品 - 這意味着,當我寫這樣 <的LinearLayout> 佈局文件它的工作沒有錯誤。但我不希望指標位於頂端,這就是爲什麼我在此情況下交換尋呼機和指標以及應用程序崩潰的原因。 – 2014-10-02 10:31:30