0

我曾经Runnable找到状态栏的高度,它给出了正确的输出。

但是如何在 的方法之外访问StatusBarHeightrun()Runnable

这是我的一段代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RelativeLayout main = (RelativeLayout) findViewById(R.id.main);

        final Rect rectgle = new Rect();
        final Window window = getWindow();

        main.post(new Runnable() {
            @Override
            public void run() {

                window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
                int StatusBarHeight = rectgle.top;
                int contentViewTop = window.findViewById(
                        Window.ID_ANDROID_CONTENT).getTop();
                int TitleBarHeight = contentViewTop - StatusBarHeight;

                Log.i("ScreenDemo", "StatusBar Height= " + StatusBarHeight
                        + " , TitleBar Height = " + TitleBarHeight);
            }
        });
}

run()PS:如果我直接在其中编写代码onCreate(),它会返回0,因为 UI 尚未呈现。
任何帮助表示赞赏。

4

2 回答 2

3

您的变量StatusBarHeight的范围是块内的本地,如果您想从 onCreate() 方法访问它,那么您需要按以下方式声明它,

private static int StatusBarHeight;  // class level declaration

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RelativeLayout main = (RelativeLayout) findViewById(R.id.main);

        final Rect rectgle = new Rect();
        final Window window = getWindow();

        main.post(new Runnable() {
            @Override
            public void run() {

                window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
                StatusBarHeight = rectgle.top;
                int contentViewTop = window.findViewById(
                        Window.ID_ANDROID_CONTENT).getTop();
                int TitleBarHeight = contentViewTop - StatusBarHeight;

                Log.i("ScreenDemo", "StatusBar Height= " + StatusBarHeight
                        + " , TitleBar Height = " + TitleBarHeight);
            }
        });
}
于 2012-11-16T10:40:06.187 回答
1

您需要在类中将变量声明为私有成员变量,以便您可以在Runnable. 像这样:

private int statusBarHeight; // member variable available to all methods in the class

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);    
    final RelativeLayout main = (RelativeLayout) findViewById(R.id.main);
    final Rect rectgle = new Rect();
    final Window window = getWindow();
    main.post(new Runnable() {
        @Override
        public void run() {
            window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
            statusBarHeight = rectgle.top;
            int contentViewTop = window.findViewById(
                     Window.ID_ANDROID_CONTENT).getTop();
            int titleBarHeight = contentViewTop - statusBarHeight;
            Log.i("ScreenDemo", "StatusBar Height= " + statusBarHeight
                     + " , TitleBar Height = " + titleBarHeight);
        }
    });
}

注意: 我已将变量的名称从StatusBarHeighttostatusBarHeight和 fromTitleBarHeight更改为titleBarHeight。请记住对变量使用初始小写名称。Java 命名约定使用初始小写的变量名和初始大写的类名。

于 2012-11-16T11:37:45.910 回答