我實現預期在垂直方向一個可行的ViewPagerIndicator。當我測試在水平方向,無論是在運行時切換(實際轉向電話),或者當應用在水平模式下啓動(電話是水平的應用程序開始前),我得到一個空白的空間,其中ViewPagerIndicator應該的。我有一個layout/main.xml和layout-land/main.xml,其中有幾個按鈕按預期方式顯示(在垂直模式下垂直堆疊,在水平模式下爲網格)。當我按下「編輯」按鈕,我啓動編輯活動:Android的 - 垂直方向的作品,水平方向空白
public class ATEditActivity extends Activity {
static final int OFFSCREEN_PAGE_LIMIT = 5;
ViewPager pager = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
int iPage = 2; // set the page based on caller's intentions
Bundle bndlExtras = getIntent().getExtras();
if (bndlExtras != null) {
iPage = bndlExtras.getInt("PAGE");
}
ATViewPagerAdapter adapter = new ATViewPagerAdapter(this);
pager = (ViewPager) findViewById(R.id.awesomepager);
pager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
pager.setAdapter(adapter);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.TitlePageIndicator);
indicator.setViewPager(pager);
indicator.setCurrentItem(iPage);
}
}
edit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp">
<EditText
android:id="@+id/etName"
android:inputType="textAutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text"
android:layout_margin="4dp"
android:hint="@string/name" />
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/TitlePageIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/Widget.MyTitlepageIndicator" />
<android.support.v4.view.ViewPager
android:id="@+id/awesomepager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
爲什麼ViewPagerIndicator不會在橫向模式下顯示上有什麼想法?是否需要更多信息?
如果我讀過onConfigurationChanged文件正確,我會實現擴展目標,並有getter和setter所有我想要存儲的數據存儲類(而不是將在的onCreate重新創建的意見和適配器)。然後在onCreate中查找存儲的數據並在找到時加載它。這是對的嗎? – Dave 2012-04-07 22:53:25
你可以這樣做的,或者如果你不關心只是存儲的全局變量在您的應用程序,你可以得到你的應用程序的引用,並訪問這樣的變量。或者,您只需在savedInstance中保存足夠的數據,以便您可以使用該信息使用相關值恢復佈局。你可以只是設置你的配置,從破壞你的活動,並再次重新啓動它停止它的另一件事,看到http://developer.android.com/reference/android/R.attr.html#configChanges – EdChum 2012-04-07 23:27:18
我終於讀懂了做對onConfigurationChanged()機制進行一些測試。當Toast顯示時,此功能被成功調用。我可以做一些工作來設置ViewPagerAdapter。當電話處於水平模式時,TitlePageIndicator仍然不顯示。我已經註釋了創建TitlePageIndicator對象的代碼,並且我沒有在任何一種模式下看到它,所以我知道我處於正確的位置。 – Dave 2012-04-08 17:29:58