我想开发这样的应用程序。这与android gallery完全一样。
所以我尝试了HorizontalScrollView
。但我仍然面临一些问题
这是我的布局。
<ImageView
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/hScrollView"/>
<HorizontalScrollView
android:id="@+id/hScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
这是我的代码
LinearLayout image;
ImageView main;
ImageView[] iv=new ImageView[images.length];
int i;
和活动,
for(i=0;i<images.length;i++)
{
iv[i]=new ImageView(this);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
iv[i].setLayoutParams(lparams);
iv[i].setImageResource(images[i]);
iv[i].setTag(images[i]);
image.addView(iv[i]);
iv[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
main.setImageResource((Integer) iv[i].getTag());
}
});
但总是给我ArrayIndexOutOfBoundException
。我知道每次迭代都会更新i
的值。
那我该怎么办?任何替代方式都是可观的。
我使用的是api等级18,因此Gallery
无效。
答案 0 :(得分:1)
你可以像这样使用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 1 :(得分:1)
试试这个..
<强> XML:强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Gallery
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<强> JAVA 强>
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
Gallery gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
参考:
http://saigeethamn.blogspot.in/2010/05/gallery-view-android-developer-tutorial.html
http://learnandroideasily.blogspot.in/2013/07/android-gallery-view-example.html
http://www.androidpeople.com/android-gallery-imageview-example
答案 2 :(得分:1)
请注意: android.widget.Gallery不再受支持。其他水平滚动小部件包括支持库中的HorizontalScrollView
和ViewPager
。
根据您的要求,HorizontalScrollView
更合适。查看以下教程,它将帮助您实施。
答案 3 :(得分:1)
试试这个:
<?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" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/hScrollView"
android:background="@drawable/ic_launcher" />
<HorizontalScrollView
android:id="@+id/hScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
在Activity类中:
int[] drawable = { R.drawable.background, R.drawable.forground,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
centerImageView = (ImageView) findViewById(R.id.imageView);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < drawable.length; i++) {
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView view = new ImageView(this);
view.setLayoutParams(lparams);
view.setId(i);
view.setImageResource(drawable[i]);
linearLayout.addView(view);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
centerImageView.setImageResource(drawable[v.getId()]);
}
});
}
}
希望能帮助你:)