Hir*_*tel 12 android android-linearlayout android-relativelayout
我需要相对/线性布局的运行时尺寸.
activity_home.xml:
<RelativeLayout
android:id="@+id/rlParent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
请分享你的想法.
use*_*882 17
试试这个
@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);
}
Run Code Online (Sandbox Code Playgroud)
您可以将 OnGlobalLayoutListener 附加到相对布局的 ViewTreeObserver ,当视图附加到窗口并为其分配实际高度时,该布局将被调用。
final RelativeLayout rl_cards_details_card_area = (RelativeLayout) findViewById(R.id.rl_cards_details_card_area);
rl_cards_details_card_area.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
int w = rl_cards_details_card_area.getWidth();
int h = rl_cards_details_card_area.getHeight();
Log.v("W-H", w + "-" + h);
rl_cards_details_card_area.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
});
Run Code Online (Sandbox Code Playgroud)
那么我已经通过这种方式实现了:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout);
ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = linearLayout.getMeasuredWidth();
int height = linearLayout.getMeasuredHeight();
}
});
Run Code Online (Sandbox Code Playgroud)
这个例子是线性布局,相对布局遵循相同的过程。
希望这会帮助你。
| 归档时间: |
|
| 查看次数: |
21177 次 |
| 最近记录: |