Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在图像查看器中的每个图像下添加文本说明_Java_Android_Android Viewpager_Layout Inflater - Fatal编程技术网

Java 在图像查看器中的每个图像下添加文本说明

Java 在图像查看器中的每个图像下添加文本说明,java,android,android-viewpager,layout-inflater,Java,Android,Android Viewpager,Layout Inflater,由于galley类已被弃用,所以我尝试用viewpager类替换它,我已经创建了我的图像viewpager,并且工作得很好 现在我尝试在每个图像下添加文本描述,我在谷歌上搜索了很多,但没有结果,我尝试使用LayoutInflater和自定义布局,但最后什么都没有,它不显示文本 注意:这是第一次使用viewpager并尝试对其进行自定义,因此我下面的代码尝试使用viewpager对布局进行充气并对其进行自定义,可能是错误的,或者需要进行更多调整,因为在这方面没有经验 如果您有任何建议,我们将不胜感

由于galley类已被弃用,所以我尝试用viewpager类替换它,我已经创建了我的图像viewpager,并且工作得很好

现在我尝试在每个图像下添加文本描述,我在谷歌上搜索了很多,但没有结果,我尝试使用LayoutInflater自定义布局,但最后什么都没有,它不显示文本

注意:这是第一次使用viewpager并尝试对其进行自定义,因此我下面的代码尝试使用viewpager对布局进行充气并对其进行自定义,可能是错误的,或者需要进行更多调整,因为在这方面没有经验

如果您有任何建议,我们将不胜感激,谢谢

我的代码:

图像寻呼机:

 public class ImagePager extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
    ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);}

      private int imageArra[] = { R.drawable.a, R.drawable.b, R.drawable.c,
                       R.drawable.d, R.drawable.e, R.drawable.f,
                                 R.drawable.g, R.drawable.h, R.drawable.i, 
                                 R.drawable.j, R.drawable.k};}
  public class ImagePagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];

public ImagePagerAdapter(Activity act, int[] imgArra) {
    imageArray = imgArra;
    activity = act;   }

public int getCount() {
    return imageArray.length;   }

public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().
          getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_pager, null);

    layout.findViewById(R.id.caption);

 ImageView view = new ImageView(activity);                      
    view.setPadding(5, 25, 5, 5);
    view.setScaleType(ScaleType.FIT_START);
    view.setBackgroundColor(Color.RED);
    ((ViewPager) collection).addView(view, 0);
    return view;   }

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);   }

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);   }

@Override
public Parcelable saveState() {
    return null;   }
                }
ImagePagerAdapter:

 public class ImagePager extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
    ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);}

      private int imageArra[] = { R.drawable.a, R.drawable.b, R.drawable.c,
                       R.drawable.d, R.drawable.e, R.drawable.f,
                                 R.drawable.g, R.drawable.h, R.drawable.i, 
                                 R.drawable.j, R.drawable.k};}
  public class ImagePagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];

public ImagePagerAdapter(Activity act, int[] imgArra) {
    imageArray = imgArra;
    activity = act;   }

public int getCount() {
    return imageArray.length;   }

public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().
          getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_pager, null);

    layout.findViewById(R.id.caption);

 ImageView view = new ImageView(activity);                      
    view.setPadding(5, 25, 5, 5);
    view.setScaleType(ScaleType.FIT_START);
    view.setBackgroundColor(Color.RED);
    ((ViewPager) collection).addView(view, 0);
    return view;   }

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);   }

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);   }

@Override
public Parcelable saveState() {
    return null;   }
                }

实例化Item()
方法中,您可以创建一个
图像视图
,并将其添加到视图寻呼机并返回它。在此方法中,创建一个垂直方向的
LinearLayout
,然后创建一个
ImageView
和一个
TextView
作为描述,然后将
图像视图
文本视图
添加到
线性布局
,将
线性布局
添加到查看页面,并在此处返回
线性布局

public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService
             (Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_pager, null);

    layout.findViewById(R.id.caption);

 ImageView view = new ImageView(activity);                      
    view.setPadding(5, 25, 5, 5);
    view.setScaleType(ScaleType.FIT_START);
    view.setBackgroundColor(Color.RED);
    ((ViewPager) collection).addView(view, 0);
    return view;   }
cutome\u pager
中,这样设计您的视图

<?xml version="1.0" encoding="utf-8"?>

@M Mohsin Naeem非常感谢您的重播,但当运行应用程序时,它只显示文本视图而不显示图像,因为黑屏只显示按钮处的文本。任何建议,Thanksy您需要为您的
图像视图
分配图像。您可以使用
im.setBackgroundColor(Color.RED)@M Mohsin Naeem ImageView=新图像视图(活动);这也不行,任何建议我亲爱的
mageView view=newimageview(活动)因为您没有将此添加到任何
视图
。看我的edits@MMohsin Naeem我亲爱的im.setBackgroundColor(颜色:红色);将显示红色背景而不是图像