看起来很简单,但不知怎的,我无法弄清楚如何找到左(x)和上(y)坐标的视图我几乎尝试了每个答案,每个人都给零。
这是xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/include_list_viewpager" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view" />
include_list_viewpager.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<AutoCompleteTextView
android:id="@+id/et_inputSearch"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_action_bar_stacked_max_height"
android:layout_marginLeft="@dimen/tiny_margin"
android:layout_marginRight="@dimen/tiny_margin"
android:layout_marginTop="@dimen/tiny_margin"
android:background="@drawable/edit_text_rounded_with_background_register"
android:completionThreshold="1"
android:drawableLeft="@drawable/ic_search_black_24dp"
android:drawablePadding="@dimen/default_margin"
android:hint="@string/msg_hint_search_text_box"
android:imeOptions="actionSearch"
android:inputType="textCapSentences|textAutoCorrect"
android:maxLines="1"
android:paddingLeft="@dimen/horizontal_margin"
android:textColor="@color/colorPrimaryText"
android:textColorHint="@color/divider"
android:textSize="@dimen/info_text_heading" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/btn_filter"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/frame_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_text_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_filter"
android:drawablePadding="@dimen/tiny_margin"
android:gravity="center"
android:text="@string/txt_filter"
android:textAllCaps="false"
android:textColor="@color/white" />
<TextView
android:id="@+id/tv_filter"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="top|left"
android:background="@drawable/badge_filter_fab"
android:gravity="center"
android:textColor="@color/white"
android:textSize="10sp"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
<View
android:layout_width="1px"
android:layout_height="20dp"
android:layout_gravity="center"
android:background="@color/white" />
<LinearLayout
android:id="@+id/btn_sort"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_sort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_swap_vert_white_24dp"
android:gravity="center"
android:text="@string/txt_sort"
android:textAllCaps="false"
android:textColor="@color/white" />
<TextView
android:id="@+id/tv_sort_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:paddingLeft="@dimen/tiny_margin"
android:text="@string/text_default"
android:textColor="@color/white"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<fragment
android:id="@+id/fragment_container"
android:name="in.workindia.nileshdungarwal.workindiaandroid.fragments.FragJobAllList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_job_list" />
<FrameLayout
android:id="@+id/fl_bottom_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin">
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onFabClick"
app:borderWidth="0dp" />
<TextView
android:id="@+id/tv_on_float"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="12dp"
android:text="NEW"
android:textColor="@android:color/white" />
</FrameLayout>
**Code snippet to find out co-ordinate**
private void showUseSearch() {
View etSearchTextList = findViewById(R.id.et_inputSearch);
int[] tableLayoutCorners = new int[2];
Rect myViewRect = new Rect();
etSearchTextList.getGlobalVisibleRect(myViewRect);
float x = myViewRect.left;
float y = myViewRect.top;
etSearchTextList.getLocationInWindow(tableLayoutCorners);
Log.i(TAG, x+","+y+"-"+tableLayoutCorners[0] + "-" + tableLayoutCorners[1] + ",showUseSearch: " + etSearchTextList.getX() + "," + etSearchTextList.getY() + "-" + etSearchTextList.getPivotX() + "-" + etSearchTextList.getPivotY());
}
答案 0 :(得分:1)
如果您在完全渲染UI之前尝试获取位置,则会将其设为零。在绑定UI之后,您可以使用treeObserver侦听器来获取x和y。你走了:
view.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Layout has happened here.
int x = (int)view.getX();// returns float
int y = (int)view.getY();
// Don't forget to remove your listener when you are done with it.
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});