我需要相对/线性布局的运行时尺寸。
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);
请分享您的想法。
答案 0 :(得分:16)
试试这个
@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);
}
答案 1 :(得分:4)
您可以将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);
}
});
答案 2 :(得分:2)
我已经通过这种方式实施了:
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();
}
});
此示例适用于线性布局,相对布局遵循相同的流程。
希望这会对你有所帮助。
答案 3 :(得分: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()));
}
}
答案 4 :(得分:0)
我们可能无法在创建活动后立即获得正确的高度和视图或布局,因为布局没有完全膨胀
所以
1.你可以在几秒之后触发一个runnable方法,然后调用一个获取width和height属性的函数。
2.我们可以使用视图树观察器
l = (RelativeLayout)findviewbyid(R.id.rl_cards_details_card_area);
ViewTreeObserver observer = l.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
ActivityClassName.this.getProperties();
l.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});
void getProperties() {
int a= l.getHeight();
int b = l.getWidth();
Toast.makeText(getActivity,""+a+" "+b,1000).show();
}
答案 5 :(得分:0)
对于Kotlin :您可以使用AndroidX的doOnLayout扩展功能来获取布局的高度或宽度
view.doOnLayout {
Timber.d("View's Height: ${view.height}")
}
布局此视图时,执行给定的操作。如果视图有 布局,并且尚未请求布局,则操作将为 立即执行,否则操作将在之后执行 接下来是视图。
该动作仅在下一个布局上被调用一次,然后 删除。 For more info