XML

2012-02-21 2 views
1

で宣言された相対的なレイアウトの大きさは、これが私のxmlです取得:XML

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
> 

    <RelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/header" 
    android:background="@android:color/white" 
    > 
     <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/btnBack" 
     android:text="Back" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     /> 

     <ImageView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:src="@drawable/icon" 
     android:layout_centerInParent="true" 
     /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_below="@+id/@+id/header" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/view" 
     android:layout_above="@+id/footer" 
    > 
    </RelativeLayout> 

    <RelativeLayout 
    android:id="@+id/footer" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_alignParentBottom="true"  
    android:background="@android:color/white"  
    > 
     <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/btn1" 
     android:text="btn1" 
     android:layout_alignParentLeft="true" 

     /> 

     <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/btn2" 
     android:text="btn2" 
     android:layout_centerHorizontal="true" 
     /> 

     <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/btn3" 
     android:text="btn3" 
     android:layout_alignParentRight="true" 
     /> 

    </RelativeLayout> 

    </RelativeLayout> 
</RelativeLayout> 

相対レイアウトビューには、2つのレイアウトの間です。 私はこの相対的なレイアウトのサイズ(つまり、高さと幅)を自分のコードで取得したいと思っています。getWidthとgetHeightを使って試しましたが、0を表示しています。 他の方法はありますか?

答えて

2
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class AndroidResizeView extends Activity { 

    TextView textMyTextView; 
    Button myButton; 

    RelativeLayout rlayout; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.hightk); 

     rlayout = (RelativeLayout) findViewById(R.id.rlayout); 

     textMyTextView = (TextView) findViewById(R.id.textView1); 

     myButton = (Button) findViewById(R.id.button1); 

     myButton.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       updateSizeInfo(); 
      } 
     }); 
    } 

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

    private void updateSizeInfo() { 

     textMyTextView.setText(String.valueOf("Width: " 
       + rlayout.getWidth() + "height ::" + rlayout.getHeight())); 

    } 

} 
+3

はこれをしようとしたが、それは私にそれがこの作品の罰金私に – droid

+0

の両方を与えた!!!! – droid

+0

チェックした結果を与えていないこのように、高さと幅は0 –

0

私の場合、RelativeLayoutから拡張して、独自のカスタムビューを作成し、onSizeChangeイベントのリスナーを追加しました。

public class MyRelativeLayout extends RelativeLayout { 

    public static interface SizeChangeListener { 
     void onSizeChanged(); 
    } 

    private SizeChangeListener mListener; 

    public MiRelativeLayout(Context context) { 
     super(context); 
    } 

    public MiRelativeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MiRelativeLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     if (w != oldw || h != oldh) { 
      if (mListener != null) { 
       mListener.onSizeChanged(); 
      } 
     } 
    } 

    public void setOnSizeChangeListener(SizeChangeListener mListener) { 
     this.mListener = mListener; 
    } 

}