I need to align View2
top-left corner to View1
's center, but I cannot figure out how to do it.
Asked
Active
Viewed 2,499 times
4

ilomambo
- 8,290
- 12
- 57
- 106
-
Have you tried doing it in programmatically or only in XML? – Ken Wolf Jun 06 '13 at 09:21
-
@KenWolf No, I haven't. I know how to do it in code, very simple, just take View1's measures and coordinates and and relocate View2 accordingly. I am trying to do it in XML. Unless there is no other choice. – ilomambo Jun 06 '13 at 09:23
-
Check these ,they might help you http://stackoverflow.com/questions/3758932/layouts-on-top-of-each-other http://stackoverflow.com/questions/2629940/how-to-layer-views http://stackoverflow.com/questions/7888337/how-do-i-put-buttons-on-top-of-each-other-in-same-xml-layout – Rachita Nanda Jun 06 '13 at 09:24
-
@ilomambo can't see how you'd do it in XML without absolutely specifying dimensions, etc...but let's see :) – Ken Wolf Jun 06 '13 at 09:25
-
@RachitaNanda thanks, but none of those were helpful to my problem – ilomambo Jun 06 '13 at 09:31
-
@KenWolf I think maybe creating a `quarterView1` and putting it invisible on top of `View1` and then aligning `View2` to its bottom & right. – ilomambo Jun 06 '13 at 09:32
-
@ilomambo sure but you'd still need to specify widths right? How big is quarterView1? How can you make it 1/4 of View1? I would just do it in code. – Ken Wolf Jun 06 '13 at 09:38
-
2@KenWolf You're probably right. It seems nobody at android's SDK team thought that `layout_toCenterOf` is a needed attribute. – ilomambo Jun 06 '13 at 09:57
3 Answers
1
Here is how to do it!
Make an xml like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#FFFF99" >
<LinearLayout
android:id="@+id/view1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:background="#440000"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/view1"
android:layout_alignTop="@id/view1"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/view2"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#99004400"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
In your activty
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
LinearLayout v1=(LinearLayout)findViewById(R.id.view1);
LinearLayout v2=(LinearLayout)findViewById(R.id.view2);
LinearLayout container=(LinearLayout)findViewById(R.id.container);
int heightV1=v1.getHeight();
int widthV1=v1.getWidth();
container.setPadding(widthV1/2, heightV1/2, 0, 0);
}
Gives output like

Arun C
- 9,035
- 2
- 28
- 42
0
try this
RelativeLayout.LayoutParams gpsViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT);
this.relativeLayout.addView(gpsView,gpsViewLayoutParams);

blganesh101
- 3,647
- 1
- 24
- 44
-
1????? Did you paste the right code? I only see one view there, aligned to its parent top-right corner. – ilomambo Jun 06 '13 at 09:28
-3
Is this of any help to you?
<RelativeLayout>
<View1 />
<View2
Layout_marginTop= (view1Height/2)+view1margin
Layout_marginLeft=(view1Width/2)+view1margin />
</RelativeLayout>

anudroid
- 163
- 4