Это мой 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. Есть ли другой способ?
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()));
}
}
В моем случае я создал свой собственный пользовательский вид, простирающийся от 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;
}
}