2014-05-19 1 views
0

두 개의 textView 개체가 있으며 그 중 하나는 "@+id/touchView"이고 다른 하나는 "@+id/viewLocat"입니다. 활동이 시작될 때 viewLocattouchView의 좌표를 표시하고 싶습니다.텍스트보기의 위치 0

이 내가 (내 .java 파일에) 지금까지 한 일이다 : 그러나

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     View touchView = findViewById(R.id.touchView); 
     TextView viewLocat = (TextView)findViewById(R.id.viewLocat); 
     int[] viewLocation = new int[2]; 
     touchView.getLocationOnScreen(viewLocation); 
     viewLocat.setText(String.valueOf(viewLocation[0]) + "x" + String.valueOf(viewLocation[1])); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

, 내가 활동을 시작할 때 텍스트 뷰의 값이 0,0입니다.

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     View touchView = findViewById(R.id.touchView); 
     TextView viewLocat = (TextView)findViewById(R.id.viewLocat); 
     viewLocat.setText(String.valueOf(touchView.getTop())); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

그러나,이 단순히 0를 반환

또한 (또한 .java 파일) 아래 그림과 다른 접근 방식을 시도했다. 나는 코드를 만들 수있는 방법 는 배치 될 때까지 LinearLayout

+0

'touchView'는 화면의 왼쪽 상단에 있습니까? – Emmanuel

+0

네, 그렇기 때문에 이것이 의미가 있다고 생각했습니다. 그러나, 나는'touchView'를 위의'View '를 추가함으로써 움직이는 것으로 테스트했지만 여전히 0,0을 반환했다. – user3616949

답변

0

로보기 올바른 정보를 반환하지 않습니다 부모와 관련하여 TextView의 좌표를 반환합니다.

관련 문제