2

我有一个带有图像的取景器,我的问题是图像(图像视图)没有居中。我已经解决了几个小时,不幸的是没有工作。我在 imageview 中找不到 setGravity 或重力。这是我不工作的代码:

我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper android:id="@+id/flipper" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"   
    android:scaleType="centerInside"
    android:background="#000000"
> 
</ViewFlipper>

在我的 src 文件中:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
     View view = inflater.inflate(R.layout.slider_image, container, false);      

     viewFlipper = (ViewFlipper)view. findViewById(R.id.flipper);
     desc = (TextView)view.findViewById(R.id.textDesc);
     desc.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum " +
            "libero dapibus mi semper accumsan. Vestibulum tortor diam, " +
            "imperdiet in consectetur faucibus, vulputate eget turpis." +
            " Nullam nec aliquet arcu. Nunc pretium nulla non nulla pulvinar " +
            "ac interdum risus dictum. Morbi viverra condimentum elit sit amet lobortis.");
     desc.setMovementMethod(new ScrollingMovementMethod());
     for(int i=0;i<mThumbIds.length;i++)
     {
         setFlipperImage(mThumbIds[i]);
     }
     viewFlipper.setAutoStart(true);
     viewFlipper.setFlipInterval(8000);
     viewFlipper.setPadding(20, 10, 20,0);
     viewFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.view_right_in));
     viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.view_right_out));
     viewFlipper.startFlipping();

     viewFlipper.setOnTouchListener(new OnTouchListener(){

         public boolean onTouch(View arg0, MotionEvent arg1) {

             switch (arg1.getAction()){

                    case MotionEvent.ACTION_DOWN:{
                        downXValue = arg1.getX();
                        break;
                    }

                    case MotionEvent.ACTION_UP:{
                        float currentX = arg1.getX();            
                        if (downXValue < currentX)
                        {
                             viewFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.view_right_in));
                             viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.view_right_out));
                             viewFlipper.showPrevious();
                        }
                        if (downXValue > currentX)
                        {
                            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.view_left_in));
                            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.view_left_out));
                            viewFlipper.showNext();
                        }
                        break;
                    }
              }
             return true;
         }
     });

     super.setMenuVisibility (true);
     super.setHasOptionsMenu(true);

     return view; 
 }

我的 setflipper 功能:

private void setFlipperImage(int res) {

    ImageView image = new ImageView(getActivity().getApplicationContext());

    image.setBackgroundResource(res);
    @SuppressWarnings("deprecation")
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    image.setLayoutParams(layoutParams);
    image.setMaxHeight(350);
    image.setMaxWidth(600);
    image.setAdjustViewBounds(true);
    image.getLayoutParams().height = 350;
    image.getLayoutParams().width = 350;
    viewFlipper.addView(image);
}

任何帮助深表感谢。

4

2 回答 2

1

尝试将 ViewFlippers 宽度设置为

    android:layout_width="wrap_content"

我有一个类似的问题,这很好

于 2012-12-06T19:17:18.510 回答
0

如何设置你ImageView这样的:

image.setScaleType(ImageView.ScaleType.FIT_CENTER);
于 2012-08-22T10:00:00.407 回答