在Android中,获取API 10上的相对布局中心

在Android中,获取API 10上的相对布局中心,android,android-relativelayout,Android,Android Relativelayout,我试图找到目前为空的RelativeLayout的中心坐标。我需要先找到中心,然后才能将任何内容放入相对布局中。相对布局不是整个屏幕,因此我无法使用度量来查找屏幕尺寸。因为我的最小API是10,所以我不能使用rLayout.getX()或rLayout.getY()。所以我尝试的是: int xScreenInt = gBoard_RL.getWidth()/2; int yScreenInt = gBoard_RL.getHeight()/2; int xInt = gBoard_RL.ge

我试图找到目前为空的
RelativeLayout
的中心坐标。我需要先找到中心,然后才能将任何内容放入相对布局中。相对布局不是整个屏幕,因此我无法使用度量来查找屏幕尺寸。因为我的最小API是10,所以我不能使用
rLayout.getX()
rLayout.getY()
。所以我尝试的是:

int xScreenInt = gBoard_RL.getWidth()/2;
int yScreenInt = gBoard_RL.getHeight()/2;

int xInt = gBoard_RL.getLeft() + xScreenInt;
int yInt = gBoard_RL.getTop() + yScreenInt;
我得到的只是0,0。有人知道我该怎么做吗?以下是我目前的布局和活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gb1_root_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".GB1" >

<RelativeLayout
    android:id="@+id/gb1_topInfo_RL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/blu_grade_bg"
    android:orientation="vertical"
    android:paddingBottom="15dp" >

    <TextView
        android:id="@+id/gb1_scValue_TV"
        style="@style/mainScoreValueStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/gb1_score_TV"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/gb1_timeValue_TV"
        style="@style/mainTimeValueStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/gb1_time_TV"
        android:layout_marginRight="10dp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/gb1_gf_RL"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/gb1_topInfo_RL"
    android:padding="10dp" >
</RelativeLayout>

在完成布局过程之前调用onCreate。所有视图都还没有大小


尝试设置全局布局侦听器,并在那里执行定位代码。

尝试将代码放入
onGlobalLayout
侦听器:

myRelativelayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        //Your code here
     }
});
编辑:
正如@xorgate所指出的那样,在onCreate方法中还没有绘制布局。绘制布局时,将调用
onGlobalLayout
方法,并在其中进行计算。

获取相对布局的中心有一个很好的技巧。空视图设置为
centerInParent
。我使用
public void getGBsize(){gBoard_RL.getViewTreeObserver().addOnGlobalLayoutListener(oGLL);}OnGlobalLayoutListener oGLL=new OnGlobalLayoutListener(){@Override public void onGlobalLayout(){int-XScreinint=gBoard_RL.getWidth()/2、 int-yscreenit=gBoard_RL.getHeight()/2;int-xInt=gBoard_RL.getLeft()+xscreenit;int-yInt=gBoard_RL.getTop()+yscreenit;xStr=String.valueOf(xInt);yStr=String.valueOf(yInt);GScore_TV.setText(xStr+“,“+yStr);}
myRelativelayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        //Your code here
     }
});