我正在尝试创建一个新的画廊风格的应用程序,因为画廊已经折旧.
我遇到的主要问题是,当我尝试运行以下代码时,我无法动摇错误“dimen无法解决或不是字段”.我已经阅读了一些网站和一些帖子,说它与导入“import com.example.test.R;”有关.然而即使有这样的错误仍然存在,如果有人可以帮助它将是惊人的.
Gall.java:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; public class Gall extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gall); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); ImgAdapt adapter = new ImgAdapt(this); viewPager.setAdapter(adapter); } }
activity_gall.xml:
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
ImgAdapt.java:
package com.example.test; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class ImgAdapt extends PagerAdapter { Context context; private int[] GalImages = new int[] { R.drawable.one,R.drawable.two,R.drawable.three }; ImgAdapt(Context context){ this.context=context; } public int getCount() { return GalImages.length; } public boolean isViewFromObject(View view,Object object) { return view == ((ImageView) object); } public Object instantiateItem(ViewGroup container,int position) { ImageView imageView = new ImageView(context); int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); imageView.setPadding(padding,padding,padding); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageView.setImageResource(GalImages[position]); ((ViewPager) container).addView(imageView,0); return imageView; } public void destroyItem(ViewGroup container,int position,Object object) { ((ViewPager) container).removeView((ImageView) object); } }
很多代码,对不起,如果这是一个模糊的问题,但行中的’维度’
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
是唯一给我一个错误的部分.