gpt4 book ai didi

android-layout - 如何获得RelativeLayout的宽度和高度

转载 作者:行者123 更新时间:2023-12-04 04:28:02 24 4
gpt4 key购买 nike

我无法获得RelativeLayout的宽度和高度,该如何获得?我的layout.xml文件是

<?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>




而且我只是使用其内置的提供的方法,通过编程获得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);

} }


谁能帮我解决这个问题?

最佳答案

override首先使用此方法。


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



尝试仅使用 getHeight()getWidth()


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

关于android-layout - 如何获得RelativeLayout的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37462680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com