你好StackOverflow! 最近我试图为Android开发一个枢轴控件,但我遇到了一些问题。 顶部菜单文本使用Gallery视图实现。此库包含一些TextView。但是我无法将它们与屏幕的左边距对齐。任何想法? 这是我的代码:
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;
}
}
}