在XML

2012-02-21 41 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> 

相對佈局視圖在兩個佈局之間。 我想在我的代碼中獲得這種相對佈局(即高度和寬度)的大小。我通過使用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

試過,但它給了我的高度和寬度0 – droid 2012-02-21 14:31:49

+0

這種方式是不是給我結果 – droid 2012-02-21 14:43:21

+0

檢查這項工作很好!!!! – 2012-02-21 14:46:18

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; 
    } 

}