添加文字描述图像viewpager每个图像下 [英] Add text description under each image in image viewpager
问题描述
随着厨房类去precated所以我尽量用viewpager类来代替它,我已经建立了我的图片viewpager 并很好地工作。
现在我试着给每个图片下添加的文字说明,我很多google搜索关于没有结果,即时通讯尝试使用 LayoutInflater 是自定义布局但什么也没有结束,它不显示文本。
注意:这是第一次使用viewpager并尝试定制,所以在下面试图充气的布局和viewpager定制,也许错了或需要更多我的code调整后,在没有任何的experiance的堂妹。
任何意见将是pciated实现这一AP $ P $,谢谢。
我的code:
ImagePager:
公共类ImagePager延伸活动{@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); ImagePagerAdapter适配器=新ImagePagerAdapter(这一点,imageArra);
ViewPager myPager =(ViewPager)findViewById(R.id.myimagepager);
myPager.setAdapter(适配器);
myPager.setCurrentItem(0);} 私人诠释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};}
ImagePagerAdapter:
公共类ImagePagerAdapter扩展PagerAdapter {活动活动;
INT imageArray [];公共ImagePagerAdapter(活动行为,INT [] imgArra){
imageArray = imgArra;
活性=行为; }公众诠释的getCount(){
返回imageArray.length; }公共对象instantiateItem(查看收集,INT位置){
LayoutInflater吹气=(LayoutInflater)collection.getContext()。
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.custom_pager,NULL); layout.findViewById(R.id.caption); ImageView的观点=新ImageView的(活动);
view.setPadding(5,25,5,5);
view.setScaleType(ScaleType.FIT_START);
view.setBackgroundColor(Color.RED);
((ViewPager)集合).addView(查看,0);
返回视图。 }@覆盖
公共无效destroyItem(查看为arg0,ARG1 INT,对象ARG2){
((ViewPager)为arg0).removeView((查看)ARG2); }@覆盖
公共布尔isViewFromObject(查看为arg0,ARG1对象){
返回将arg0 ==((查看)ARG1); }@覆盖
公共Parcelable saveState和(){
返回null; }
}
在这里
公共对象instantiateItem(查看收集,INT位置){
LayoutInflater吹气=(LayoutInflater)collection.getContext()。getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.custom_pager,NULL); layout.findViewById(R.id.caption); ImageView的观点=新ImageView的(活动);
view.setPadding(5,25,5,5);
view.setScaleType(ScaleType.FIT_START);
view.setBackgroundColor(Color.RED);
((ViewPager)集合).addView(查看,0);
返回视图。 }
在 cutome_pager
设计您查看这个样子。
<?XML版本=1.0编码=UTF-8&GT?;
< ImageView的
机器人:ID =@ + ID / gallery_imageView
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/><的TextView
机器人:ID =@ + ID / gallery_imageView_dsc
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:文字=你好....../>
和
公共对象instantiateItem(查看收集,INT位置){
LayoutInflater吹气=(LayoutInflater)collection.getContext()。getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.custom_pager,NULL); ImageView的IM =(ImageView的)layout.findViewById(R.id.gallery_imageView);
im.setBackgroundColor(Color.RED);
TextView的DSC =(TextView中)layout.findViewById(R.id.gallery_imageView_dsc);
((ViewPager)集合).addView(布局,0);
返回布局; }
As galley class deprecated so i try to substitute it with viewpager class , i already created my image viewpager and work nicely .
now im trying to add text description under each image , i googled alot about that with no result ,im try to use LayoutInflater with custom layout but end with nothing , it doesnt show the text .
note: this is the first time to use viewpager and try to customize it , so my code below in attempt to inflater the layout and customize it with viewpager, maybe wrong or need more adjustment ,coz of no experiance in that .
any advice will be appreciated to achieve that ,thanks.
My Code :
ImagePager:
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};}
ImagePagerAdapter:
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; }
}
here
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; }
In cutome_pager
design your View like this.
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/gallery_imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/gallery_imageView_dsc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello...." />
and
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);
ImageView im=(ImageView) layout.findViewById(R.id.gallery_imageView);
im.setBackgroundColor(Color.RED);
TextView dsc=(TextView) layout.findViewById(R.id.gallery_imageView_dsc);
((ViewPager) collection).addView(layout, 0);
return layout; }
这篇关于添加文字描述图像viewpager每个图像下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!