布局:
<?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:background="#ff0000" tools:context="me.chunsheng.hongbao.activities.GallaryActivity"> <ImageView android:id="@+id/displayImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" /> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/mygallery" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> </HorizontalScrollView> </RelativeLayout>
代码:
package me.chunsheng.hongbao.activities; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import java.io.IOException; import java.io.InputStream; import me.chunsheng.hongbao.R; public class GallaryActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallary); final ImageView diplayImage = (ImageView) findViewById(R.id.displayImage); final LinearLayout myGallery = (LinearLayout) findViewById(R.id.mygallery); try { String galleryDirectoryName = "gallery"; String[] listImages = new String[3]; for (int i = 0; i < 4; i++) { //InputStream is = getAssets().open(galleryDirectoryName + "/" + imageName); //final Bitmap bitmap = BitmapFactory.decodeStream(is); ImageView imageView = new ImageView(getApplicationContext()); imageView.setLayoutParams(new ViewGroup.LayoutParams(700, 700)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setImageResource(R.mipmap.mayun); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diplayImage.setImageResource(R.mipmap.bg_action_bar); } }); myGallery.addView(imageView); } } catch (Exception e) { } } }