ImageView白页仅显示图像

时间:2014-09-03 07:00:12

标签: android android-activity imageview

我使用适配器的问题是什么,我的imageview没有显示我的图像。它只是在平板电脑上给我空白页面。我已经被困在这里3个小时不能做任何关于我的图像没有出现在平板电脑的landsacpe模式但在我的手机显示但其拉伸 这是我的图像代码

package com.example.jcw;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    int[] flag;
    String[] rank;
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context,  int[] flag, String[] rank) {
        this.context = context;
        this.flag = flag;
        this.rank = rank;
    }

    @Override
    public int getCount() {
        return flag.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView imgflag;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.app3x, container,
                false);



        // Locate the ImageView in viewpager_item.xml
        imgflag = (ImageView) itemView.findViewById(R.id.flag);

        // Capture position and set to the ImageView
        imgflag.setImageResource(flag[position]);

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:background="@color/White"

>

<ImageView
    android:id="@+id/flag"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:background="@color/White"
    android:scaleType="fitXY" />

这是我使用我的适配器的地方

    package com.example.jcw;


import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.view.WindowManager;

import com.viewpagerindicator.UnderlinePageIndicator;

public class activity3 extends Activity 
{


    ViewPager viewPager;
    PagerAdapter adapter;
    int[] flag;
    String[] rank;
    UnderlinePageIndicator mIndicator;




    @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.app3);


       rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14" , "15" ,"16" , "17" , "18" };


       flag = new int[] { R.drawable.gr, R.drawable.calvitc,
               R.drawable.barleyjuice, R.drawable.hilifejuice, R.drawable.calvitc2guys, R.drawable.calvitcteddy,
               R.drawable.kapesupremo, R.drawable.glutafit, R.drawable.omniwhitelotion, R.drawable.omniwhitebbcream ,
               R.drawable.omniwhitepinkishcream , R.drawable.omniwhitesoap, R.drawable.omniwhitetoner,
               R.drawable.organicbarley, R.drawable.premieregreentea, R.drawable.shuya, R.drawable.omniwhitecream,
               R.drawable.organicspirulina };

    // Locate the ViewPager in viewpager_main.xml
    viewPager = (ViewPager) findViewById(R.id.pager);
    // Pass results to ViewPagerAdapter Class
    adapter = new ViewPagerAdapter(activity3.this, flag , rank);
    // Binds the Adapter to the ViewPager
    viewPager.setAdapter(adapter);

    // ViewPager Indicator
    mIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
    mIndicator.setFades(false);
    mIndicator.setViewPager(viewPager);




    }






}

1 个答案:

答案 0 :(得分:-1)

package com.androidbegin.viewpagertutorial;


  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.view.ViewPager;

 public class MainActivity extends Activity
{

ViewPagerAdapter adapter;
String[] rank;

int[] flag;
ViewPager viewPager;

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    // Get the view from viewpager_main.xml
    setContentView(R.layout.viewpager_main);
    // Generate sample data
    rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };


    flag = new int[] { R.drawable.china, R.drawable.india,
            R.drawable.unitedstates, R.drawable.indonesia,
            R.drawable.brazil, R.drawable.pakistan, R.drawable.nigeria,
            R.drawable.bangladesh, R.drawable.russia, R.drawable.japan };

    // Locate the ViewPager in viewpager_main.xml
    viewPager =  (ViewPager) findViewById(R.id.pager);
    // Pass results to ViewPagerAdapter Class
    adapter = new ViewPagerAdapter(MainActivity.this,  flag,rank);

    // Binds the Adapter to the ViewPager
    viewPager.setAdapter(adapter);

  }
 }





package com.androidbegin.viewpagertutorial;


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

  public class ViewPagerAdapter extends PagerAdapter 
   {
 // Declare Variables
Context context;
String[] rank;

int[] flag;
LayoutInflater inflater;

public ViewPagerAdapter(Context context, int[] flag,String[] rank ) {
    this.context = context;
    this.rank = rank;

    this.flag = flag;
}

@Override
public int getCount() {
    return rank.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    // Declare Variables
    TextView txtrank;

    ImageView imgflag;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);

    Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();

    // Locate the TextViews in viewpager_item.xml
    txtrank = (TextView) itemView.findViewById(R.id.rank);

    // Capture position and set to the TextViews
    txtrank.setText(rank[position]);

    // Locate the ImageView in viewpager_item.xml
    imgflag = (ImageView) itemView.findViewById(R.id.flag);
    // Capture position and set to the ImageView
    imgflag.setImageResource(flag[position]);

    // Add viewpager_item.xml to ViewPager
    ( container).addView(itemView);



    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ( container).removeView((RelativeLayout) object);

}
}

// viewpager_main.xml

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

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

</RelativeLayout>

// viewpager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >

<TextView
    android:id="@+id/ranklabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ranklabel" />

<TextView
    android:id="@+id/rank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/ranklabel" />

<ImageView
    android:id="@+id/flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="#000000"
    android:padding="1dp" />