Android панорама и управление поворотом - PullRequest
1 голос
/ 08 августа 2011

Здравствуйте, на StackOverflow! Недавно я пытался разработать сводный контроль для Android, но у меня возникли некоторые проблемы. Текст верхнего меню реализован в виде галереи. Эта галерея содержит несколько TextViews. Однако я не могу установить их выравнивание по левому краю экрана. Любая идея? Вот мой код:

    public class LeftGallery extends Activity {

private Gallery gallery;

protected int left,right,top,bottom; 

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

     gallery = (Gallery) findViewById(R.id.gallery1);
     DisplayMetrics metrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metrics);



     MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
     left=mlp.leftMargin;
     right=mlp.rightMargin;
     top=mlp.topMargin;
     bottom=mlp.bottomMargin;
     TextView menuOpts[]=new TextView[4];
     menuOpts[0]=new TextView(this);menuOpts[0].setText("LARGE WORD");
     menuOpts[1]=new TextView(this);menuOpts[1].setText("small");
     menuOpts[2]=new TextView(this);menuOpts[2].setText("a");
     menuOpts[3]=new TextView(this);menuOpts[3].setText("OTHER");
     gallery.setAdapter(new WP7TopMenu(this, android.R.layout.simple_spinner_item, menuOpts));
     gallery.setAnimationDuration(1000);
     gallery.setSpacing(10);
     int adjustBy=((WP7TopMenu)gallery.getAdapter()).getItem(0).getWidth();
     mlp.setMargins(left, top, right, bottom);
     Log.d("LOGGY", mlp.leftMargin+"");
     int computedscroll=adjustBy;
     mlp.setMargins( -((computedscroll)), 
                    mlp.topMargin, 
                    mlp.rightMargin, 
                    mlp.bottomMargin
     );
     gallery.setSelection(0);
     Log.d("LOGGY", mlp.leftMargin+"");

     gallery.setOnItemSelectedListener(
             new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    int adjustBy=((WP7TopMenu)gallery.getAdapter()).getItem(arg2).getWidth();
                    //Log.d("LOGGY", adjustBy+"");
                    DisplayMetrics metrics2 = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metrics2);



                    MarginLayoutParams mlp2 = (MarginLayoutParams) gallery.getLayoutParams();

                    mlp2.setMargins(left, top, right, bottom);
                    Log.d("LOGGY", mlp2.leftMargin+"");


                    int computedScroll=metrics2.widthPixels+adjustBy;

                    mlp2.setMargins( -((computedScroll)), 
                                   mlp2.topMargin, 
                                   mlp2.rightMargin, 
                                   mlp2.bottomMargin
                    );
                    Log.d("LOGGY", mlp2.leftMargin+"");

                }

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

                }

    });
     gallery.setSelection(0);

}
protected class WP7TopMenu extends ArrayAdapter<TextView>
{



    public WP7TopMenu(Context context, int textViewResourceId,
            TextView[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub


    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView v=getItem(position);
        v.setTextSize(50);
        return v;
    }

}


}
...