2015-05-31 92 views
0

我有觀點認爲,由xml中的android:layout_centerHorizo​​ntal =「true」。我如何獲取左邊的邊緣,以便我可以將其他視圖與它對齊?如何在android中獲取居中視圖的左邊距?

我試着在居中視圖上使用getLayoutParams,但它沒有奏效。它似乎報告左邊0. 0.

我也試着用(screenWidth-view width)/ 2來計算leftMargin,但是它給出了比預期略高的值。更具體地說,這是我的代碼:

Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    int screenWidth = size.x; 
    int screenHeight = size.y; 

    TextView button1 = (TextView) findViewById(R.id.button1); 
    RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams) 
    button1.getLayoutParams(); 
    params.height=102; 
    params.width=114; 
    params.topMargin =0; 
    button1.setLayoutParams(params); 
    params=(RelativeLayout.LayoutParams) button1.getLayoutParams(); 
    int leftMargin = params.leftMargin; 

    TextView button2 = (TextView) findViewById(R.id.button2); 
    params=(RelativeLayout.LayoutParams) button2.getLayoutParams(); 
    params.height=102; 
    params.width=114; 
    params.leftMargin =leftMargin; 
    params.topMargin =102; 
    button2.setLayoutParams(params); 

    TextView button3 = (TextView) findViewById(R.id.button3); 
    params=(RelativeLayout.LayoutParams) button3.getLayoutParams(); 
    params.height=102; 
    params.width=114; 
    params.leftMargin =(screenWidth-114)/2; 
    params.topMargin =204; 
    button3.setLayoutParams(params); 

Button1以xml爲中心。 Button2是左對齊的,不居中。 Button3或多或少居中,但稍向右。

所以,

1)我如何才能在XML爲中心的視圖左側的保證金?

2)我該如何在代碼中精確定位視圖? getDefaultDisplay報告的寬度是否比實際屏幕寬度高?

+0

你試過' android:layout_alignLeft'或'android:layout_alignStart'?您可以通過指定其ID來使用此視圖與您希望與居中視圖對齊的視圖。 – razzledazzle

回答

0

您可以直接詢問佈局以使視圖對齊。例如,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/centered_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Random text"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/centered_view" 
     android:layout_alignParentTop="true" 
     android:text="Top Button"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/centered_view" 
     android:layout_alignParentBottom="true" 
     android:text="Bottom Button"/> 
</RelativeLayout> 

編輯: 要獲得相對中心的觀點的偏差,這可能就足夠了:

textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      Log.d(getClass().getSimpleName(), "top: " + textView.getTop() + " left: " + textView.getLeft()); 
      textView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     } 
    }); 

參見:

View.getTop()View.getLeft()

+0

好的,這個工作,它很有用。但是,它並不完全回答我的問題。如果我想要另一個視圖,讓我們說在居中視圖的右側200像素,我需要知道居中視圖的左邊距。那麼,我如何從視圖本身獲得它,或者如何計算它呢? – JMCaia

+0

你的意思是你希望視圖在居中視圖之前?你可以使用'android:layout_toLeftOf'或'android:layout_toRightOf'並設置一些邊距? – razzledazzle

+0

不,我需要知道居中視圖左邊界的確切值,以便我可以在其他地方使用它。 – JMCaia

相關問題