如何为水平图像滚动列表分配背景容器

时间:2013-04-10 11:52:55

标签: android android-layout horizontalscrollview

我想显示水平图像滚动列表,所以我搜索谷歌,我发现非常好的教程,但他们没有解释为水平列表实现动态容器的想法。

我想在水平列表的开头显示一个图像,在列表的末尾显示一个图像。(如箭头图像) 我想为我的图像滚动列表设置图像容器 它非常混乱,特别像我一样新鲜。 我被困在这一点上解释我想要的东西我正在附加一个图像和我当前的代码。

感谢任何帮助。请指导我。 谢谢你宝贵的时间。

这是主要活动代码

MyHorizontalLayout myHorizontalLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myHorizontalLayout = (MyHorizontalLayout)findViewById(R.id.mygallery);
    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory()
            .getAbsolutePath();
    String targetPath = ExternalStorageDirectoryPath + "/test/";
    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
    File targetDirector = new File(targetPath);
     File[] files = targetDirector.listFiles();
    for (File file : files){
    myHorizontalLayout.add(file.getAbsolutePath());
    }    
}

这是我的Horizo​​ntalLayout代码

public class MyHorizontalLayout extends LinearLayout {
Context myContext;
ArrayList<String> itemList = new ArrayList<String>();
public MyHorizontalLayout(Context context) {
    super(context);
    myContext = context;
}
public MyHorizontalLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    myContext = context;
}
public MyHorizontalLayout(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    myContext = context;
}
void add(String path){
    int newIdx = itemList.size();
    itemList.add(path);
    addView(getImageView(newIdx));  
}
ImageView getImageView(final int i){
    ImageView imageView = new ImageView(myContext);
    imageView.setLayoutParams(new LayoutParams(150, 150));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    Bitmap bm = null;
    if (i < itemList.size()){
        bm = decodeSampledBitmapFromUri(itemList.get(i), 150, 150);
    }
    imageView.setImageBitmap(bm);
    imageView.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(myContext, 
                    "Clicked - " + itemList.get(i), 
                    Toast.LENGTH_LONG).show();
        }});

    return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
    Bitmap bm = null;
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options); 
    return bm;  
}
public int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);    
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);      
        }   
    }
    return inSampleSize;    
}

这是XML文件

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp" >
    <com.example.test.MyHorizontalLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

horizontal list

1 个答案:

答案 0 :(得分:0)

使用相对布局作为基础放置背景(滚动视图背景),在此内部使用framelayout添加水平布局和两个箭头。祝你好运!