Android支持ViewPager不可见

标签 android android-layout android-viewpager

我已经在我的应用程序中实现了 Android 支持库中的 ViewPager,但由于某种原因它现在不可见。应用程序布局有点嵌套,我确信这会导致一些问题。我会尝试将其压缩为有意义的代码。 我的布局膨胀代码:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

View v = getLayoutInflater().inflate(R.layout.project_row, null);

TextView header = (TextView) v.findViewById(R.id.rowheader);
header.setText(rowHeader);

ViewPager projectRow = (ViewPager) v.findViewById(R.id.pager);
projectRow.setAdapter(new ProjectPagerAdapter(tiles));

LinePageIndicator lineIndicator = (LinePageIndicator)v.findViewById(R.id.pagerIndicator);
lineIndicator.setViewPager(projectRow);
lineIndicator.setStrokeWidth(7);
lineIndicator.setLineWidth(50);
lineIndicator.setSelectedColor(Color.parseColor("#555555"));
lineIndicator.setUnselectedColor(Color.parseColor("#DCDCDC"));

ll.addView(v);

project_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<TextView
    android:id="@+id/rowheader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/SectionHeaderColor" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="280dp" />

<com.viewpagerindicator.LinePageIndicator
    android:id="@+id/pagerIndicator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

如果我使用此通货膨胀代码,ViewPager 会自行工作:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

View v = getLayoutInflater().inflate(R.layout.pageronlytest, null);
ViewPager vp = (ViewPager)v.findViewById(R.id.pager);
vp.setAdapter(new ProjectPagerAdapter(tiles));

ll.addView(v);

pageronlytest.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="280dp"
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="280dp" />

</LinearLayout>

最佳答案

project_row.xml 中,您没有显式声明 LinearLayout 的方向,这意味着它将默认为“水平”。由于第一个元素 (TextView) 定义了 match_parent/fill_parent 宽度,因此它将有效地将所有其他元素推到右侧并离开屏幕。

话虽这么说,您可能只需将 android:orientation="vertical" 添加到 LinearLayout 就可以了。

关于Android支持ViewPager不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11251236/

相关文章:

java - 一段时间后声音停止 - Android

java - 有效的 JSON 仍然出现解析错误

Android webview 在方向改变时保留表单数据

android - Android Support4Demos 的 FragmentTabs 示例中的两个框架布局

java - Android 中 Dialog Builder 中的运行时异常

android - 使用 tabLayout 和 ViewPager 未在 Fragment 中调用 OnResume()

android - 将带有滑动选项卡的 viewpager 作为嵌套格式放置在 fragment 中

android - 没有谷歌分析实现 android 的谷歌播放 Activity 跟踪

android - 选择图像时 onActivityResult() 返回 Activity 的第一个 fragment

Android Material View Pager GridView 布局设计不正确