【问题标题】:Android - vertical orientation works, horizontal orientation blankAndroid - 垂直方向有效,水平方向空白
【发布时间】:2012-04-20 20:01:03
【问题描述】:

我正在实现一个在垂直方向上按预期工作的 ViewPagerIndicator。当我在水平方向进行测试时,通过在运行时切换(物理转动手机)或当应用程序以水平模式启动时(在应用程序启动之前手机是水平的),我会在 ViewPagerIndicator 应该是的空白处得到一个空白区域。我有一个 layout/main.xml 和 layout-land/main.xml ,其中有几个按预期显示的按钮(在垂直模式下垂直堆叠,在水平模式下作为网格)。当我按下“编辑”按钮时,我启动了编辑活动:

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 不会以水平模式显示有任何想法吗?需要更多信息吗?

【问题讨论】:

    标签: android android-viewpager android-orientation


    【解决方案1】:

    您可能需要覆盖onConfigurationChanged() 请参阅reference,因为您的应用需要再次检索资源并绘制所有内容。

    【讨论】:

    • 如果我正确阅读了 onConfigurationChanged 文档,我将实现一个扩展 Object 的存储类,并为我想要存储的所有数据提供 getter 和 setter(但不会重新创建视图和适配器在 onCreate 中)。然后在 onCreate 中查找存储的数据并在找到时加载它。对吗?
    • 您可以这样做,或者如果您不关心在您的应用程序中存储全局变量,您可以获得对您的应用程序的引用并以这种方式访问​​变量。或者您在 savedInstance 中保存足够的数据,以便您可以使用该信息来恢复具有相关值的布局。您可以做的另一件事是设置您的配置以阻止它破坏您的活动并再次重新启动它,请参阅developer.android.com/reference/android/…
    • 我终于开始对 onConfigurationChanged() 机制进行一些测试。在显示 Toast 时成功调用此函数。我可以做一些工作来设置 ViewPagerAdapter。当手机处于水平模式时,TitlePageIndicator 仍然不显示。我已经注释掉了创建 TitlePageIndicator 对象的代码,并且在任何一种模式下都没有看到它,所以我知道我在正确的位置。
    • @Imhotep 这是一个很好的进展,听起来你在正确的轨道上,了解应用程序生命周期对于处理用户操作可以修改应用程序的多种方式至关重要。
    • 通过我一直在做的所有调试,我可以看到 TitlePageIndicator 正在创建并且所有默认值都在设置。但是代码永远不会进入 TitlePageIndicator.onDraw()。我不了解 TitlePageIndicator 和 ViewPager 如何交互的所有内部原理,但我认为问题出在两者之间。即使应用程序以水平模式启动,TPI 也不会显示。在启动应用程序之前,我已经在水平模式下和直接在水平模式下的手机上确认了相同的行为。
    【解决方案2】:

    在尝试了我能想象到的一切三四天后,我终于确定我的项目中一定有什么问题,所以我创建了一个新项目,该项目只包含使 ViewPagerIndicator 正常工作所需的最少代码量。新项目现在正在使用“开箱即用”的两个方向。我不知道是什么原因导致我的旧项目只在水平方向上出现问题,但我会让新项目走上正轨并尽快加快速度,因为它正在按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2023-02-21
      相关资源
      最近更新 更多