android - 如何以编程方式获取相对的宽度和高度 - android中的线性布局?

标签 android android-linearlayout android-relativelayout

我需要相对/线性布局的运行时尺寸。

activity_home.xml:

<RelativeLayout
    android:id="@+id/rlParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

HomeActivity.java:

private int width, height;

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

w = rlParent.getWidth();
h = rlParent.getHeight();

Log.i("Width-Height", width+"-"+height);

请分享您的想法。

最佳答案

试试这个

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

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

关于android - 如何以编程方式获取相对的宽度和高度 - android中的线性布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547974/

相关文章:

android - 如何从手机浏览器打开深层链接?

Android 如何在发布前获取 Google Play 商店链接

java - ClipDrawable 在用作小部件的布局背景时不起作用

Android 相对布局选择器

android - 动画 RelativeLayout 边距变化

android - 以编程方式将 LinearLayout 的项目设置在中心屏幕

android - 语音频率如何检测?

java - OSMDROID OnItemGestureListener

android - 小部件纵横比 1 :1 (square)

android.widget.ProgressBar 无法转换为 android.widget.LinearLayout