代码如下,但我在创建相对布局时遇到了一些问题(它是linearLayout的子代)。我认为我正在创建两个文本视图(设置两个ID)并正确设置每个文本视图。
但是,第一个textview不居中,第二个textview没有被绘制到屏幕上(它应该在menuTitle下面)。请让我知道我在哪里错了。代码中的LinearLayout,RelativeLayout位于主RelativeLayout(在setContentView行中注明)内。我的所有自定义类都只是覆盖draw方法,在视图周围绘制一个白色边框。我不会在这些子类中更改布局参数。(我注意到有些人在执行此操作时遇到了问题)。
作为旁注 - 这些将动态创建,这就是编程路由而不是XML的原因。无论如何,请提前感谢所有人的帮助!
CustomRelativeLayout subMenuLayout = new CustomRelativeLayout(this);
subMenuLayout.setBackgroundColor(Color.TRANSPARENT);
RelativeLayout.LayoutParams subMenuLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
subMenuLayoutParams.addRule(RelativeLayout.RIGHT_OF, sideMenu.getId());
subMenuLayoutParams.setMargins(0, 0, 200, 0);
subMenuLayout.setLayoutParams(subMenuLayoutParams);
TextView menuItemTitle = new CustomTextView(this);
menuItemTitle.setText("All You Can Eat");
menuItemTitle.setTextSize(30);
menuItemTitle.setBackgroundColor(Color.TRANSPARENT);
menuItemTitle.setTextColor(Color.WHITE);
menuItemTitle.setId(2);
RelativeLayout.LayoutParams menuItemTitleParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
menuItemTitleParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
menuItemTitleParams.setMargins(0, 0, 0, 15);
menuItemTitle.setLayoutParams(menuItemTitleParams);
TextView menuItemDesc = new CustomTextView(this);
menuItemDesc.setText("All you can ribs, chicken, pork and sides you can stomach to eat\nAlso includes dessert!");
menuItemDesc.setTextSize(15);
menuItemDesc.setBackgroundColor(Color.TRANSPARENT);
menuItemDesc.setTextColor(Color.WHITE);
menuItemDesc.setId(3);
RelativeLayout.LayoutParams menuItemDescParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
menuItemDescParams.addRule(RelativeLayout.BELOW, menuItemTitle.getId());
menuItemDesc.setLayoutParams(menuItemDescParams);
subMenuLayout.addView(menuItemTitle);
subMenuLayout.addView(menuItemDesc);
RelativeLayout screenLayout = new RelativeLayout(this);
screenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
screenLayout.setBackgroundResource(R.drawable.body_bkgd);
screenLayout.addView(sideMenu);
screenLayout.addView(subMenuLayout);
setContentView(screenLayout);
}
答案 0 :(得分:1)
我认为您需要将TextView
封装在垂直方向LinearLayout
中。将TextView
添加到LinearLayout而不是RelativeLayout
,然后将LinearLayout
添加到RelativeLayout
并添加所需的位置参数。这是一个例子:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation( LinearLayout.VERTICAL );
TextView menuItemTitle = new CustomTextView(this);
menuItemTitle.setText("All You Can Eat");
menuItemTitle.setTextSize(30);
menuItemTitle.setBackgroundColor(Color.TRANSPARENT);
menuItemTitle.setTextColor(Color.WHITE);
LinearLayout.LayoutParams menuItemTitleParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
menuItemTitleParams.gravity = Gravity.CENTER_HORIZONTAL;
menuItemTitle.setLayoutParams(menuItemTitleParams);
TextView menuItemDesc = new CustomTextView(this);
menuItemDesc.setText("All you can ribs, chicken, pork and sides you can stomach to eat\nAlso includes dessert!");
menuItemDesc.setTextSize(15);
menuItemDesc.setBackgroundColor(Color.TRANSPARENT);
menuItemDesc.setTextColor(Color.WHITE);
menuItemDesc.setLayoutParams(menuItemTitleParams);
linearLayout.addView(menuItemTitle);
linearLayout.addView(menuItemDesc);
答案 1 :(得分:0)
无法评论,所以尽管没有回答你的问题,但必须将其放在答案中。
您不必仅仅因为您的视图是动态创建的而完成所有代码 - 您可以直接获取inflater并夸大layout.xml文件:
LayoutInflater inflater = LayoutInflater.fromContext(getContext())
View layout = inflater.inflate(R.layout.some_layout, parentView, false);
// Any other set up here.
parentView.addView(layout);
这可能对您的问题有所帮助,因为它可能有助于识别错误嵌套的视图或类似信息。