Android - getX和getY返回0

时间:2017-10-08 15:54:05

标签: android

美好的一天! 我很难掌握我的一些控件的协调性。 我想澄清一下,控件位于父活动中显示的片段内。我需要这个在LinearLayout上运行。

我已经读过问题在于我们执行检索的顺序, 所以我确实尝试使用view.post(new Runnable) and getTranslationX(),它产生了相同的结果。

其他参考文献将不胜感激。谢谢。

  1. getHeight returns 0 for all Android UI objects

  2. How to retrieve the dimensions of a view?

  3. View getX() and getY() return 0.0 after they have been added to the Activity

  4. 修改: 我发现了一些令人震惊的事情。 我决定为什么不尝试将代码附加到一个按钮,看看我们是否可以获得任何值,当我执行并运行应用程序然后按下按钮时,我得到0!到底是怎么回事?

    修改: 将API级别从9更改为11.产生相同的结果。

    以下是代码:

    //this class extends fragment
        @SuppressLint("NewApi")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.game_sh_view, container, false);
    
            view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //1.    Fetch the controls
                    getControls();
    
                    //2.    Add function to the controls
                    setControlFunctions();
    
                    //3.    Execute on Init settings
                    getScreenSize();
                    locateControls();
                    threadingMethods();
    
                    //4.    Misc onInit commands
                    onInitAesthetics();
                }
            });
    
            return view;
        }
    
        float screenSizeX;
        float screenSizeY;
        @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
        public void getScreenSize(){
            WindowManager wm = getActivity().getWindowManager();
            Display disp = wm.getDefaultDisplay();
            Point size = new Point();
            disp.getSize(size);
            screenSizeX = size.x;
            screenSizeY = size.y;
            sop("=============SCREEN SIZE==============");
            sop("Screen Size X: "+size.x);
            sop("Screen Size Y: "+size.y);
            sop("============SCREEN SIZE END===========");
        }
        @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
        public void locateControls(){
            comLeftYLocation = comLeft.getY();
            comLeftYPermLocation = comLeft.getY();
            sop("=============comLeft LOCATOR==============");
            sop("comLeft Location Y: "+comLeft.getY());
            sop("============comLeft LOCATOR END===========");
        }
    

    XML布局:

    <LinearLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        tools:context="layout.game_sh"
        android:orientation="vertical">
    
        <!-- TODO: Update blank fragment layout -->
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            tools:ignore="UselessParent">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:gravity="center"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/comLeft"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/hand_left_down"
                tools:ignore="ContentDescription"
                tools:layout_editor_absoluteX="103dp"
                tools:layout_editor_absoluteY="-13dp" />
    
            <ImageView
                android:id="@+id/comRight"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/hand_right_down"
                tools:ignore="ContentDescription"
                tools:layout_editor_absoluteX="191dp"
                tools:layout_editor_absoluteY="8dp" />
        </LinearLayout>
    
        <Space
            android:layout_width="match_parent"
            android:layout_height="100dp"
            tools:ignore="NewApi" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:gravity="center"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/playerLeft"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/hand_left_up"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:id="@+id/playerRight"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/hand_right_up"
                tools:ignore="ContentDescription" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            tools:ignore="UselessParent">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/wood"
                app:srcCompat="@drawable/h_f"
                tools:ignore="ContentDescription" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
    
            <Button
                android:id="@+id/l_trigger"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lefttrigger"
                tools:ignore="ButtonStyle" />
    
            <Button
                android:id="@+id/r_trigger"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/righttrigger"
                tools:ignore="ButtonStyle" />
        </LinearLayout>
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textview" />
    
    </LinearLayout>
    

    我的控制台中的结果:

    [ 10-08 23:27:39.511 30284:30406 W/         ]
                                                                               fimg library or stretchFimgApi_v5 symbol can not be loaded
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: =============SCREEN SIZE==============
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: Screen Size X: 1080
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: Screen Size Y: 1920
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: ============SCREEN SIZE END===========
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: =============comLeft LOCATOR==============
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: comLeft Location Y: 0.0
    10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: ============comLeft LOCATOR END===========
    

0 个答案:

没有答案