ImageView显示空白图像

时间:2016-07-08 03:25:13

标签: android android-viewpager imageview

我尝试在ViewPager下显示图片,ViewPager xml代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ljl.com.homuraproject.Activity.GuideActivity"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/guide_linear_layout"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/guide_button"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@null"
        android:text=" "
        />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/guide_linear_layout">
    <android.support.v4.view.ViewPager
        android:id="@+id/guide_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
        <android.support.v4.view.PagerTabStrip
            android:id="@+id/guide_pagertab"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>
</RelativeLayout>

ImageView xml代码如下:     

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/material_blue_grey_800">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:id="@+id/guide_image_1"
        android:src="@drawable/bg"
        />
</RelativeLayout>

,活动中的代码如下:

private void InitViewAdapter() {
    guideView_1 = getLayoutInflater().from(this).inflate(R.layout.guide_image_1, null);
    guideView_2 = getLayoutInflater().from(this).inflate(R.layout.guide_image_2, null);
    imageViewList = new ArrayList<View>();
    imageViewList.add(guideView_1);
    imageViewList.add(guideView_2);
    final ArrayList<String> titleList = new ArrayList<String>();
    titleList.add("1");
    titleList.add("2");
    viewPager = (ViewPager) this.findViewById(R.id.guide_viewpager);
    PagerAdapter guide_pager_adapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return imageViewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(imageViewList.get(position));
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViewList.get(position));
            return imageViewList.get(position);
        }
    };
    viewPager.setAdapter(guide_pager_adapter);
}

我试图在代码中启动imageview,但它也没有用。 我已经困在这个问题好几天了,我无法弄清楚我哪里做错了,请帮忙。

1 个答案:

答案 0 :(得分:1)

使用此视图代码

替换此代码
<android.support.v4.view.ViewPager
    android:id="@+id/guide_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/guide_pagertab"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

使用这个新代码

<android.support.v4.view.ViewPager
        android:id="@+id/guide_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/guide_pagertab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>