【发布时间】:2012-03-05 14:39:24
【问题描述】:
目前我已尝试将我的线性布局对齐到左侧和中心,但目前无法使其正常工作。见以下代码:
CustomLinearLayout sideMenu = new CustomLinearLayout(this);
RelativeLayout.LayoutParams sideMenuParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
sideMenuParams.addRule(RelativeLayout.CENTER_VERTICAL);
sideMenuParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
sideMenu.setLayoutParams(sideMenuParams);
sideMenu.setOrientation(LinearLayout.VERTICAL);
sideMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sideMenu.setBackgroundColor(Color.TRANSPARENT);
//sideMenu.setGravity(Gravity.CENTER);
sideMenu.addView(AppSoups);
sideMenu.addView(salads);
sideMenu.addView(ribs);
sideMenu.addView(favorites);
sideMenu.addView(sandwiches);
sideMenu.addView(sides);
sideMenu.addView(desserts);
RelativeLayout screenLayout = new RelativeLayout(this);
screenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
screenLayout.setBackgroundResource(R.drawable.body_bkgd);
screenLayout.addView(sideMenu);
setContentView(screenLayout);
CustomLinearLayout 只是一个扩展 LinearLayout 来做一些自定义绘图的类。无论如何我在这里做错了什么?到目前为止,它向左对齐并与相对布局的顶部对齐,但我无法使其居中。正如旁注一样,我的规则中的任何一个都不能与将 LinearLayout 设置为中心一起使用(我已在代码中将其注释掉)。最后,我添加到线性布局的视图只是文本视图 - 仅供参考(代码未显示)
【问题讨论】:
标签: android android-linearlayout android-relativelayout