我一直在努力让这一天全天工作,并最终通过设置一些边距(见代码)让它工作,但我不知道它为什么会起作用
//should this be a try/catch?
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Kefa.ttc");
LinearLayout sampleLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
sampleLinearLayout.setOrientation(LinearLayout.VERTICAL);
sampleLinearLayout.setBackgroundColor(Color.WHITE);
sampleLinearLayout.setLayoutParams(layout);
TextView menuItemTitle = new TextView(this);
menuItemTitle.setId(2);
menuItemTitle.setText("All You Can Eat\t\t $13.99");
menuItemTitle.setTextSize(35);
menuItemTitle.setBackgroundColor(Color.TRANSPARENT);
menuItemTitle.setTypeface(tf);
menuItemTitle.setTextColor(Color.argb(255, 77, 30, 16));
RelativeLayout.LayoutParams menuItemTitleParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
menuItemTitleParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
//****why does setting the TOP margin work??****
menuItemTitleParams.setMargins(0, 50, 0, 0);
menuItemTitle.setLayoutParams(menuItemTitleParams);
TextView menuItemDesc = new TextView(this);
menuItemDesc.setText("All you can ribs, chicken, pork and sides you can stomach to eat\n\tAlso includes dessert!");
menuItemDesc.setTextSize(15);
menuItemDesc.setBackgroundColor(Color.TRANSPARENT);
menuItemDesc.setTextColor(Color.argb(255, 77, 30, 16));
menuItemDesc.setLayoutParams(menuItemTitleParams);
RelativeLayout.LayoutParams menuItemDescParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
menuItemDescParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
//If i comment out the line of code below, the margin above still places the text
//below the title.
menuItemDescParams.addRule(RelativeLayout.BELOW, menuItemTitle.getId());
//##menuItemTitle.setLayoutParams(menuItemDescParams);
menuItemDesc.setLayoutParams(menuItemDescParams);
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundColor(Color.TRANSPARENT);
RelativeLayout.LayoutParams subMenuLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
subMenuLayoutParams.setMargins(400, 0, 400, 0);
relativeLayout.setLayoutParams(subMenuLayoutParams);
relativeLayout.addView(menuItemTitle);
relativeLayout.addView(menuItemDesc);
sampleLinearLayout.addView(relativeLayout);
setContentView(sampleLinearLayout);
如果我没有为menuItemTitle TextView的TOP设置边距,则menuItemDesc Textview文本位于屏幕上menuTitle文本的顶部。
答案 0 :(得分:0)
我自己解决了这个问题,如评论中所述