Reputation: 2941
The code is below but I am having some issues with creating a relative layout(which is a child to a linearLayout). I think that i am creating both textviews correctly(setting up both IDs) and setting the layoutparams for each correctly.
However, the first textview isn't centered and the 2nd textview isn't being drawn to the screen(it should be below the menuTitle). Please let me know where i am going wrong here. The LinearLayout, RelativeLayout in the code is inside a main RelativeLayout(which is noted in the setContentView line). All my custom classes do is simply override the draw method to draw a white border around the view. I DO NOT change the layout parameters at all in these subclasses.(i noticed some people were having issues doing this).
As a side note - these are going to be create dynamically so thats the reason for the programmatic route instead of XML. Anyway thanks all for the help in advance!
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);
}
Upvotes: 0
Views: 634
Reputation: 1201
Unable to comment, so this has to be placed in an answer despite not actually answering your question.
You don't have to go through all that code just because your views are being created dynamically - you can get hold of an inflater and inflate a layout.xml file directly:
LayoutInflater inflater = LayoutInflater.fromContext(getContext())
View layout = inflater.inflate(R.layout.some_layout, parentView, false);
// Any other set up here.
parentView.addView(layout);
This may help with your problem, since it may help identify a mis-nested view or similar.
Upvotes: 0
Reputation: 31283
I think you need to envelop your TextView
s in a vertically oriented LinearLayout
. Add both of your TextView
s to a LinearLayout instead of the RelativeLayout
, then add the LinearLayout
to the RelativeLayout
with the position parameters you want. Here's an example:
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);
Upvotes: 1