【问题标题】:Android support ViewPager is invisibleAndroid 支持 ViewPager 是不可见的
【发布时间】:2012-06-30 08:41:51
【问题描述】:

我已经在我的应用程序中实现了来自 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>

【问题讨论】:

    标签: android android-layout android-viewpager


    【解决方案1】:

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

    话虽如此,您可能只需将android:orientation="vertical" 添加到LinearLayout 即可。

    【讨论】:

    • 完美运行!不敢相信我没有看到。
    猜你喜欢
    • 2015-01-22
    • 2013-01-11
    • 2018-03-18
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2017-04-27
    • 2020-02-29
    相关资源
    最近更新 更多