答案 0 :(得分:0)
您可以通过添加布局观察者来注册装饰视图更改。
public class MainActivity extends AppCompatActivity {
private ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d("Main Activity", "" + getWindow().getDecorView().getHeight());
Log.d("Main Activity", "" + getWindow().getDecorView().getWidth());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
}
}
...
答案 1 :(得分:0)
为我工作:
Activity yourActivity = ...
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int windowWidth = displayMetrics.widthPixels;