I have a Relative layout with title centered and cancel button right aligned. I want to check if the title overlaps with the cancel button and if so, i will have shift the title left based of how much it overlaps.

This is how my xml looks:

<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>

And I'm trying to get the positions like below

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

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

cancelButton.getX() is returning me 0.0 whereas title is returning correct value.

1.This is how the layout is with small title

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

有帮助吗?

解决方案

it depends on where in your Java code you're attempting to get the value of getX()

If Android has not already completed drawing the entire layout, cancelButton has not been drawn and the X is 0.0.

I've found that getting the value in onCreate() or onCreateView() is very easy with a post and runnable

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

        }
    });

this ensures the button has been fully drawn before you attempt to use the value

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top