java - 在一项 Activity 中添加两个 GalleryView

标签 java android xml

我想向我的应用程序添加两个图库 View 。为了弄清楚我的问题,如下图所示。我想在 ImageView 下方添加另一个水平 ScrollView ,该 View 也将显示图像,并且功能与上面的完全一样

这是我迄今为止想出的代码。它与开发人员示例相同示例。只需对其进行一些细微的更改。

public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;

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
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery1 = (Gallery)findViewById(R.id.examplegallery1);
    gallery1.setAdapter(new AddImgAdp(this));

    imgView = (ImageView)findViewById(R.id.ImageView01);    
    imgView.setImageResource(Imgid[0]);


     gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            imgView.setImageResource(Imgid[position]); 
        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgView = new ImageView(cont);

        imgView.setImageResource(Imgid[position]);
        imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

}

下面是我的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" >
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

我不能为一个 AddImg 适配器设置两个图库 View 吗?

我哪里出错了?第二个水平 ScrollView 未显示。

如何解决这个问题?

最佳答案

我认为问题出在你的布局实现上。尝试在 ImageView 上设置 android:layout_weight="1" 。我认为这会解决问题。修改后的布局如下:

 <?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" >

            <Gallery
                android:id="@+id/examplegallery"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/ImageView01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:layout_weight="1"/>

            <Gallery
                android:id="@+id/examplegallery1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

关于java - 在一项 Activity 中添加两个 GalleryView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8849549/

相关文章:

java - 多线程程序中出现意外结果

JAVA TLS 1.2 降级到 TLS 1.0 有时会报错

Android DataBinding setVariable() 后跟 getVariable() 调用返回 null

android - 为什么说PendingIntent.getBroadcast()中的requestCode没有被使用?

android - 将颜色设置为 Android 中 TextView 内的 drawableTop 图像

java - 从不同的 XML 数据集中反序列化具有相同名称的 XML @Element 和 @ElementList

java - 在android中创建菜单/子菜单

java - 如何在使用 javaassist 创建 .class 文件时向方法添加 @Override 注释?

java - 为什么我无法在 Windows 上使用 Java 从下载的源正确写入 PNG 文件?

java - 通过单击 ActionBar 按钮更改 FragmentPagerAdapter 中 fragment 的内容