线性布局内部的相对布局(里面有2个文本视图)可以工作 - 但我不知道为什么

时间:2012-03-07 04:35:40

标签: android relativelayout android-linearlayout

我一直在努力让这一天全天工作,并最终通过设置一些边距(见代码)让它工作,但我不知道它为什么会起作用

    //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文本的顶部。

  1. 为什么我的menuItemDescParams规则不足以支持BELW
  2. 如果我们不关心#1(我这样做),为什么扩展menuItemTitle边缘的TOP移动menuItemTitle textview下面的menuItemDesc?如果我注释掉BELOW的代码行,设置边距仍然有效。想想看,我认为#1非常重要

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,如评论中所述