2012-01-13 20 views
0

私のアプリケーションに2つのギャラリービューを追加したいと思います。私の質問は、下の画像に示すように明確にする。私はと同じように画像と機能を表示する画像ビューの下に別の水平スクロールビューを追加したいと思います。1つのアクティビティで2つのギャラリービューを追加する

ここで私は、これまでに作ってみたコードです。同じの例デベロッパーサンプルです。それにちょっとした変更があります。以下は

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> 

2つのギャラリービューを1つのAddImgアダプタに設定できませんか?

どこが間違っていますか? 2番目の水平スクロールビューが表示されません。

この問題を回避するにはどうすればよいですか?

+0

あなたは現在どのような問題に直面していますか?例外が発生しているか、予期しない動作が発生しているか、実行するとどうなるか教えてください。 –

+0

@AdilSoomro下の2番目の水平ビューが表示されません。私はそれを指摘していただきありがとうございます。 –

+0

'ImageView'には' android:layout_weight = "1" 'が必要です。 –

答えて

1

私は問題があなたのレイアウトの実装にあると思っています。画像ビューで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> 
+0

あなたの絶対に正しい!その問題を解決しました。何を意味するのか教えていただけますか? –

+1

android:layout_weight LinearLayoutの余分なスペースが、これらのLayoutParamsに関連付けられたビューにどれだけ割り当てられるかを示します。ビューを引き伸ばすべきでない場合は0を指定します。そうしないと、余分なピクセルは、重みが0より大きいすべてのビューの中でプロ・レートになります。 – Basil

関連する問題