为什么我无法获得按钮的位置?

时间:2015-03-06 04:03:16

标签: android android-layout android-activity android-fragments

我的班级是片段。按钮位于线性布局内。我试图找出如何获得按钮的位置,以便我可以设置一个突出显示按钮的教程动画。我正在测试getLocationInWindowgetLocationOnScreen

public class SetupContactsFragment extends Fragment {
...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_type_interactive_contacts, container, false);
...

        ibContact1 = (ImageButton) view.findViewById(R.id.contact_picker_button1);
...

        int test1[] = new int[2];
        view.getLocationInWindow(test1);

        int test2[] = new int[2];
        view.getLocationOnScreen(test2);

        System.out.println(test1[1] + " " + test2[1]);
        Log.e(">>>>", "test1[1] "+test1[1] + " test2[1] " + test2[1]);
...

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:background="#000000">

    <ScrollView
        android:id="@+id/wizard_start_root"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/wizard_static_panel">

            <TextView
                android:id="@+id/fragment_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/page_title_text"
                android:textSize="32sp"
                android:paddingBottom="5dp"
                android:layout_centerHorizontal="true"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/fragment_intro"
                style="@style/wizard_intro_style"
                android:layout_below="@+id/fragment_title" />

            <LinearLayout
                android:id="@+id/ll_fragment_warning"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                android:layout_below="@+id/fragment_intro">

                <ImageView
                    android:layout_height="50dp"
                    android:layout_width="50dp"
                    android:background="@drawable/warning" />

                <TextView
                    android:id="@+id/fragment_warning"
                    style="@style/wizard_warning_style"
                    android:textColor="#ffffff" />
            </LinearLayout>


            <LinearLayout
                android:id="@+id/ll_contact_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ll_fragment_warning"
                android:layout_marginTop="10dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/first_contact"
                    style="@style/contact_number" />

                <EditText
                    android:id="@+id/contact_edit_text1"
                    android:layout_height="52dp"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:hint="@string/contact_hint_text"
                    android:textSize="20sp"
                    android:inputType="phone" />

                <ImageButton
                    android:id="@+id/contact_picker_button1"
                    android:layout_height="48dp"
                    android:layout_width="wrap_content"
                    android:layout_weight="0"
                    android:src="@drawable/socialperson"
                    android:contentDescription="@string/choose_contact"
                    style="@style/contact_select_button" />

                <!--<fragment
                    android:name="com.mayday.md.common.ContactPickerFragment"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:id="@+id/first_contact" />-->
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_contact_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ll_contact_1"
                android:orientation="horizontal">

                <TextView
                    android:layout_height="fill_parent"
                    android:layout_width="wrap_content"
                    android:text="@string/second_contact"
                    style="@style/contact_number" />
...
</LinearLayout>

logcat就在这里

03-05 22:53:10.811  27460-27460/com.mayday.md D/AbsListView﹕ Get MotionRecognitionManager
03-05 22:53:10.811  27460-27460/com.mayday.md I/System.out﹕ 0 0
03-05 22:53:10.811  27460-27460/com.mayday.md E/>>>>﹕ test1[1] 0 test2[1] 0
03-05 22:53:10.821  27460-27460/com.mayday.md E/>>>>﹕ onActivityCreated fragmentManager FragmentManager{43ba1ce0 in SetupContactsFragment{43ba1888}}

1 个答案:

答案 0 :(得分:1)

onCreateView()期间未对您的观看次数进行衡量或调整,因此您无法在该方法中获得该观点的位置。相反,你应该在它的布局后获得一个View位置。

您可以将OnGlobalLayoutListener添加到View并在onGlobalLayout()方法中获取其位置。这是代码示例:

final View view = ...; // The view of which you want to get the location
view.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {

      @Override
      public void onGlobalLayout() {
        // here you can get the View's location
        int test1[] = new int[2];
        view.getLocationInWindow(test1);

        int test2[] = new int[2];
        view.getLocationOnScreen(test2);
      }
    });