Frage

I have been struggling to get this to work all day and finally got it to work by setting some margins(see code) but i do not know why it works

    //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);

If i don't set the margins for the TOP of the menuItemTitle TextView the menuItemDesc Textview text lays right on top of the menuTitle text on the screen.

  1. Why isnt my menuItemDescParams rule for BELOW not sufficent here?
  2. If we didn't care about #1(which i do), why is expanding the TOP of the menuItemTitle margin move the menuItemDesc below the menuItemTitle textview?? If i comment out the line of code for BELOW, setting the margin still takes effect. Come to think of it i guess #1 is pretty important
War es hilfreich?

Lösung

i solved this myself as noted in the comments

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top