如何在横向模式下设置图库

时间:2012-10-01 11:39:24

标签: android android-gallery

我是android的新手。我的应用程序库小部件完全以纵向模式运行但是在横向模式下没有正确显示。我在创建方法中使用纵向和横向模式的开关盒。

我的代码如下。

public class ViewAppGalleryActivity extends Activity {
    // take an array of integer type and set its value
    Integer[] mImageIds = { R.drawable.gallery_photo_19, R.drawable.gallery_photo_20,
            R.drawable.gallery_photo_21, R.drawable.gallery_photo_22, R.drawable.gallery_photo_23,
            R.drawable.gallery_photo_24, R.drawable.gallery_photo_25,R.drawable.gallery_photo_15,R.drawable.gallery_photo_16,R.drawable.gallery_photo_18,
            R.drawable.gallery_photo_9,R.drawable.gallery_photo_10,R.drawable.gallery_photo_11,R.drawable.gallery_photo_12,R.drawable.gallery_photo_26,
            R.drawable.gallery_photo_27, R.drawable.gallery_photo_28,R.drawable.gallery_photo_29, R.drawable.gallery_photo_30
            };

    int lastOrientation = 0;
    private int imageWidth;
    private ImageView leftArrowImageView;
    private int selectedImagePosition = 0;
    private ImageView rightArrowImageView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        switch (this.getResources().getConfiguration().orientation)
        {
        case Configuration.ORIENTATION_PORTRAIT:
            setContentView(R.layout.main);
            // take an instance of Gallery


             DisplayMetrics metrics = new DisplayMetrics();
             getWindowManager().getDefaultDisplay().getMetrics(metrics);


             final ExtendedGallery g = (ExtendedGallery)findViewById(R.id.gallery);
           //  g.setScrollingEnabled(false);
             MarginLayoutParams mlp = (MarginLayoutParams)g.getLayoutParams();
             mlp.setMargins(-(metrics.widthPixels/2 +60 ), mlp.topMargin,
                         mlp.rightMargin, mlp.bottomMargin);
            // set adapter to gallery calling ImageAdapter class
            g.setAdapter(new ImageAdapter(this, mImageIds));
            // set onItemClickListener on gallery Object





            g.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    ImageView imgView=(ImageView)findViewById(R.id.imageView1);
                    imgView.setImageResource(mImageIds [position]);

                    // TODO Auto-generated method stub
                    // onItemClick show Message
                    //selectedImagePosition =position;
                    if (selectedImagePosition > 0 && selectedImagePosition <mImageIds.length - 3) {

                                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));

                                       } 
                    else if (selectedImagePosition == 0)
                                       {

                                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
                                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
                                       }
                    else if (selectedImagePosition == mImageIds.length - 3 || selectedImagePosition == mImageIds.length - 2 || selectedImagePosition == mImageIds.length - 1) 
                                       {
                                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));

                                       }

                                   }
                              });

            g.setOnItemSelectedListener(new OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                    selectedImagePosition = pos;

                    if (selectedImagePosition > 0 && selectedImagePosition <mImageIds.length - 3) 
                                        {

                                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));

                                        } 
                    else if (selectedImagePosition == 0) 
                                        {

                                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
                                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
                                        }
                    else if (selectedImagePosition == mImageIds.length - 3 || selectedImagePosition == mImageIds.length - 2 || selectedImagePosition == mImageIds.length - 1 )
                                        {
                                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));

                                        }


                                }

                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                                                                    }
                        });


            leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
            rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);

            leftArrowImageView.setOnClickListener(new OnClickListener() {

                                                public void onClick(View v) {

                                                    if (selectedImagePosition > 2) {

                                                                                --selectedImagePosition;
                                                                                --selectedImagePosition;
                                                                                --selectedImagePosition;

                                                    }
                                                    else if(selectedImagePosition == 2)
                                                    {
                                                                            --selectedImagePosition;
                                                                            --selectedImagePosition;
                                                    }
                                                    else if(selectedImagePosition == 1)
                                                    {
                                                                            --selectedImagePosition;
                                                    }


                                                     g.setSelection(selectedImagePosition, false);
                                                    //g.scrollBy(20, 0);

                                                        //leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));



                                                }
                                            });

            rightArrowImageView.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    if (selectedImagePosition < mImageIds.length-3 )
                                            {
                                                ++selectedImagePosition;
                                                ++selectedImagePosition;
                                                ++selectedImagePosition;
                                            }

                                            g.setSelection(selectedImagePosition, false);

                                            //rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
                                        }
                                    });



          break;
        case   Configuration.ORIENTATION_LANDSCAPE:


            setContentView(R.layout.land);
             //setContentView(R.layout.main);
                // take an instance of Gallery


                 DisplayMetrics metrics1 = new DisplayMetrics();
                 getWindowManager().getDefaultDisplay().getMetrics(metrics1);


                 final ExtendedGallery g1 = (ExtendedGallery)findViewById(R.id.gallery);
               //  g.setScrollingEnabled(false);
                 MarginLayoutParams mlp1 = (MarginLayoutParams)g1.getLayoutParams();
                 mlp1.setMargins(-(metrics1.widthPixels/2 +60 ), mlp1.topMargin,
                             mlp1.rightMargin, mlp1.bottomMargin);
                // set adapter to gallery calling ImageAdapter class
                g1.setAdapter(new GalleryImageAdapter1(this, mImageIds));
                // set onItemClickListener on gallery Object





                g1.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        ImageView imgView=(ImageView)findViewById(R.id.imageView1);
                        imgView.setImageResource(mImageIds [position]);

                        // TODO Auto-generated method stub
                        // onItemClick show Message
                        //selectedImagePosition =position;
                        if (selectedImagePosition > 0 && selectedImagePosition <mImageIds.length - 3) {

                                            leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));

                                           } 
                        else if (selectedImagePosition == 0)
                                           {

                                            leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
                                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
                                           }
                        else if (selectedImagePosition == mImageIds.length - 3 || selectedImagePosition == mImageIds.length - 2 || selectedImagePosition == mImageIds.length - 1) 
                                           {
                                            leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));

                                           }

                                       }
                                  });

                g1.setOnItemSelectedListener(new OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                        selectedImagePosition = pos;

                        if (selectedImagePosition > 0 && selectedImagePosition <mImageIds.length - 3) 
                                            {

                                            leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));

                                            } 
                        else if (selectedImagePosition == 0) 
                                            {

                                            leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
                                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
                                            }
                        else if (selectedImagePosition == mImageIds.length - 3 || selectedImagePosition == mImageIds.length - 2 || selectedImagePosition == mImageIds.length - 1 )
                                            {
                                            leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));

                                            }


                                    }

                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                                                                        }
                            });


                leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
                rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);

                leftArrowImageView.setOnClickListener(new OnClickListener() {

                                                    public void onClick(View v) {

                                                        if (selectedImagePosition > 2) {

                                                                                    --selectedImagePosition;
                                                                                    --selectedImagePosition;
                                                                                    --selectedImagePosition;

                                                        }
                                                        else if(selectedImagePosition == 2)
                                                        {
                                                                                --selectedImagePosition;
                                                                                --selectedImagePosition;
                                                        }
                                                        else if(selectedImagePosition == 1)
                                                        {
                                                                                --selectedImagePosition;
                                                        }


                                                         g1.setSelection(selectedImagePosition, false);
                                                        //g.scrollBy(20, 0);

                                                            //leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));



                                                    }
                                                });

                rightArrowImageView.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {

                        if (selectedImagePosition < mImageIds.length-3 )
                                                {
                                                    ++selectedImagePosition;
                                                    ++selectedImagePosition;
                                                    ++selectedImagePosition;
                                                }

                                                g1.setSelection(selectedImagePosition, false);

                                                //rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
                                            }
                                        });







          break;

        default:
            try {
                throw new Exception("Unexpected orientation enumeration returned");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          break;
        }




                    }

1 个答案:

答案 0 :(得分:0)

  1. 在'res'目录下的layout和layout-land文件夹中创建一个main.xml。
  2. 确保您在横向上有不同的对齐方式以符合您的需要。
  3. Android系统会选择方向变化的运行时间。