2013-10-22 2 views
0

제목이 중앙 정렬되고 취소 버튼이 오른쪽 정렬 된 상대 레이아웃이 있습니다. 제목이 취소 버튼과 겹치지 않았는지 확인하고 싶으면 겹치기 정도에 따라 제목을 왼쪽으로 옮깁니다.보기의 위치 얻기

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res/com.app.mobile" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="@dimen/actionBarHeight" 
    android:layout_alignParentTop="true" 
    android:id="@+id/actionbar" 
    android:background="#F8F8F8"> 

    <com.app.mobile.subview.CustomButton android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:id="@+id/cancel_btn" 
     android:text="Cancel" 
     app:typeface="fonts/HelveticaNeue" 
     app:customStyle="Regular" 
     android:textSize="@dimen/titleTextSize" 
     android:textColor="#378BFB" 
     android:background="@android:color/transparent" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:visibility="gone"/> 

    <com.app.mobile.subview.CustomButton android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:id="@+id/share_btn" 
     app:typeface="fonts/HelveticaNeue" 
     app:customStyle="Regular" 
     android:textColor="#378BFB" 
     android:visibility="gone"/> 

    <com.app.mobile.subview.CustomTextView 
     android:id="@+id/title" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:typeface="fonts/HelveticaNeue" 
     app:customStyle="Medium" 
     android:textSize="@dimen/titleTextSize" 
     android:textColor="#000" 
     android:text="Test Title"/> 
</RelativeLayout> 

그리고 제목이 올바른 반환되는 반면

float xPos = screenTitle.getX(); 
    float titleEnd = xPos + screenTitle.getWidth(); 
    xPos = cancelButton.getX(); 

    if(titleEnd > xPos){ 
     Log.e("Title","Title overlaps cancel button"); 
    } 

cancelButton.getX은() 나에게 0.0을 반환 아래 같은 위치를 얻으려고 :

이 내 XML의 모습입니다 값.

1.이

레이아웃 작은 제목 방법입니다 그것은 당신의 자바 코드에 당신이 getX()

안드로이드 만약의 값을 얻기 위해 시도하고 위치에 따라 달라집니다

http://i.stack.imgur.com/3TFdg.jpg

+0

2.with 매우 큰 제목 http://i.stack.imgur.com/8aT2M.jpg 3.이 매우 큰 제목 http로보고하도록되어 방법은 다음과 같습니다 //i.stack .imgur.com/YNufR.jpg – Uma

답변

1

레이아웃 전체의 묘화가 완료하고 있지 않는 경우, cancelButton는 묘화되지 않고, X는 0.0입니다.

난 당신이 값을 사용하기 전에에서 onCreate() 또는 onCreateView()의 값을 얻는 것은 매우 게시물에 간단하고 실행 가능한

cancelButton.post(new Runnable() { 
    @Override 
    public void run() { 
      float x = cancelButton.getX(); 

     } 
    }); 

이 버튼을 보장 완전히 인출 된 것으로 나타났습니다

+0

효과가있었습니다! 감사!! – Uma