Android: How can I define a 45° angle of the view (RelativeLayout) in a LinearLayout
-
15-11-2019 - |
Question
I want to display a line with text on it but it shouldn't be vertical or horizontal it should be in 45° angle. I'm using for a RelativeLayout which contains a TextView and a View ( for the line). With the help of the RelativeLayout.LayoutParams I try to define the x/y position of the RelativeLayout. The horizontal display already works but how can I change it to become a 45° angle?
RelativeLayout branch_layout = (RelativeLayout) this
.findViewById(R.id.createrelativelayout);
LinearLayout branch_item = new LinearLayout(this);
branch_item.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
branch_item.setOrientation(1);
TextView tv = new TextView(this);
tv.setLayoutParams(new RelativeLayout.LayoutParams(pos, pos));
tv.setId(id);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setPadding(20, 0, 20, 0);
tv.setText(branch_name);
tv.setOnClickListener(this);
Rect bounds = new Rect();
Paint textPaint = tv.getPaint();
textPaint.getTextBounds(branch_name, 0, branch_name.length(), bounds);
View underline = new View(this);
underline.setLayoutParams(new LayoutParams(bounds.width() + 40, 1));
underline.setBackgroundColor(Color.parseColor("#000000"));
branch_item_layout = new RelativeLayout(this);
branch_item_layout.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Display display = getWindowManager().getDefaultDisplay();
//x
params.leftMargin = display.getWidth()/2;
//y
params.topMargin = ((display.getHeight()/2)-(((LinearLayout) this
.findViewById(R.id.createlinearlayout3)).getWidth()/2)-48);
//end x
params.bottomMargin = params.leftMargin + bounds.width();
//end y
params.rightMargin = (int) (params.leftMargin - (bounds.width()*Math.sin(45)));
branch_item.addView(tv);
branch_item.addView(underline);
branch_item_layout.addView(branch_item, params);
branch_layout.addView(branch_item_layout);
The whole screen implementation is only for "landscape" x 0-800, y 0-440 Right now the
first x = 400
first y = 114
end x = 434
end y = 371
Planned was to display the String and line together in 45° angle. The length of the line orientates on the String width.
Hope anyone can help me with this dilemma,
Saskia
Solution
the problem because of the x and y position was that the relativelayout became very huge. So I removed the layout branch_item_layout and add the parameter and animation stuff on branch_item.
See you,
Saskia