2016-05-26 6 views
3

RelativeLayoutの幅と高さを取得できません。どのように取得できますか?マイlayout.xmlファイルはRelativeLayoutの幅と高さを取得するには

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/layoutMain" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="50dp" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:layout_gravity="center_horizontal" 
     android:src="@drawable/ic_launcher" /> 
</RelativeLayout> 

であり、私はちょうど、誰もがこの問題で私を助けることができる内蔵の提供方法のように、

public class CustomShowcaseActivity extends Activity { static float width; 
    static float height; RelativeLayout relMain; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_custom_showcase); 

    relMain= (RelativeLayout) findViewById(R.id.layoutMain); 


    width = relMain.getMeasuredWidth(); 
     height = relMain.getMeasuredHeight(); 

    width = relMain.getWidth(); 
    height = relMain.getHeight(); 

    System.out.println("=== Width : " + width); 
    System.out.println("=== height : " + height); 

} } 

を使用してprogramtically RelativeLayoutの高さと幅を取得します?

答えて

3

overrideこの方法が最初です。

@Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     // TODO Auto-generated method stub 
     super.onWindowFocusChanged(hasFocus); 
     getRelativeLayoutInfo(); 
    } 

uが私はすでにこれを使用しますが、それは動作していないだろう、私の上記のコードを見ることができるようにだけgetHeight()getWidth()

private void getRelativeLayoutInfo(){ 
    relMain = (RelativeLayout) findViewById(R.id.layoutMain); 
    w = relMain.getWidth(); 
    h = relMain.getHeight(); 
    Log.v("W-H", w+"-"+h); 
} 
+0

を使用してみてください。 –

+0

'オーバーライド' onWindowFocusedChanged'を参照してください。 –

+0

@GauravMandlikは働いているか、私に知らせていませんか? –

関連する問題