【问题标题】:Filling a layout of X images填充 X 图像的布局
【发布时间】:2013-04-04 17:40:09
【问题描述】:

我正在尝试用许多图像填充 LinearLayout/Relative,无论如何。无法控制。

这是我的实现:

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header"
        android:layout_centerHorizontal="true" >

        <LinearLayout
            android:id="@+id/marcasImages"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
</ScrollView>

所以我多次添加同一张图片:

for (int i=0; i<10; i++){
    ImageView imatgeExemple = new ImageView(this.getApplicationContext());
    imatgeExemple.setImageResource(R.drawable.imageexample);
    contenidorImatgesGaleria.addView(imatgeExemple);
}

contenidorImatgesGaleriamarcasImages

但我得到的结果是单行 X 图像(X = 其分辨率可以填充的尽可能多的图像)。因此,例如,如果我循环 1k 次,则只显示 4 张图像。

我希望有尽可能多的行。

这是正在发生的事情:

这就是我想要实现的目标:

我尝试使用GridLayout,但没有任何改变。

<GridLayout
   android:id="@+id/marcasImages"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
</GridLayout>

知道我应该使用哪种布局吗?

【问题讨论】:

标签: android android-layout


【解决方案1】:

您可以使用GridView 来实现您想要做的事情。

GridView 很像 ListView。您有一个适配器(可以是服装)并设置了适配器。

因此,您需要:

将 GridView 添加到您的布局中

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

定义一个包含图像的服装适配器

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        // if it's not recycled, initialize some attributes
        if (convertView == null) {  
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7 };
}

设置一个 costum 适配器,在具有您的布局的 Activity 中保存图像

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

请注意,我从我提供的链接中恢复并复制粘贴。那里有更多信息。

【讨论】:

  • 没问题。我相信你会在那里找到一切。最难的部分是要知道需要什么样的控制。我只是粘贴,因为页面可以移动或什么的。
  • 好的,实现它并且它正在工作。正是我需要的。初学者很难知道在学习的早期阶段使用哪种控件哈哈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多