에서 버튼의 GET 크기가 나는 XML 파일 (DP에서) 크기를 설정하는 버튼이있는 경우 :잘못된 값을 다음과 같이
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="80dp"
android:layout_marginBottom="80dp"
android:layout_centerInParent="true"
>
<Button
android:id="@+id/btn1"
android:layout_width="10dp"
android:layout_height="10dp"
android:scaleType="fitCenter"/>
<Button
android:id="@+id/btn2"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"/>
</FrameLayout>
</RelativeLayout>
내가 버튼의 크기를 얻을 수 getWidth() and getHeight() of View returns 0에서 답을 따랐다. 그러나 10dp 및 50dp 대신 잘못된 값 300,300
을 반환합니다. 내 코드는 어떻게됩니까? 감사합니다 모두
Button btn=(Button) findViewById(R.id.btn1);
btn.measure(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
width_btn = btn.getMeasuredWidth();
height_btn = btn.getMeasuredHeight();
Log.d("TAG", String.valueOf(index)+String.valueOf(width_btn)+","+String.valueOf(height_btn));
'getMeasured *()'메소드가 아닌'getWidth()'와'getHeight()'를 사용해야합니다. 또한'Button'에서'measure()'를 호출하면 안됩니다. –
해결책을 제공해 주시겠습니까? 'onWindowFocusChanged'에서'getWidth()'와'getHeight()'메소드를 호출하려했으나 어플리케이션이'onWindowFocusChanged'로 이동하지 않았습니다 – Jame
그래, 그 방법이 싫어. 내가 발견했다. 저는 항상 __2에 설명 된 방법을 사용합니다. 레이아웃 큐에 runnable을 추가하십시오 : [이 대답은] (http://stackoverflow.com/a/24035591)의 View.post() __. 'Button','post()'에'Runnable'을 찾아 내서 그 너비/높이를 가져옵니다. 예를 게시 할 수 있습니다. 나중에 작동하도록 할 수 없으면 먼저 시도해보십시오. 내 대답은 실제로 그 예와 거의 흡사합니다. –