简体   繁体   中英

Android - vertical orientation works, horizontal orientation blank

I am implementing a ViewPagerIndicator that works as expected in vertical orientation. When I test in horizontal orientation, either by switching at run-time (physically turning the phone) or when the app starts in horizontal mode (phone was horizontal before the app started) I get a blank white space where the ViewPagerIndicator should be. I have a layout/main.xml and layout-land/main.xml with a few buttons that are displaying as expected (stacked vertically in vertical mode, as a grid in horizontal mode). When I press the "edit" button, I launch the edit activity:

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>

Any thoughts on why the ViewPagerIndicator would not display in horizontal mode? Is more information needed?

You probably need to override onConfigurationChanged() see reference as your app needs to retrieve the resources again and draw everything.

After three or four days of trying everything I could imagine, I finally decided there must have been something really messed up in my project so I created a new project that only had the minimal amount of code necessary to get the ViewPagerIndicator working. The new project is now working with both orientations "out-of-the-box". I have no idea what caused my old project to go haywire with only the horizontal orientation but I will get the new project on track and up to speed in short order now that it is working as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM