2012-11-16 3 views
0

Runnable을 사용하여 상태 표시 줄의 높이를 찾고 올바르게 출력하고 있습니다.할당 된 메서드 외부에서 개인 변수에 액세스

그러나 Runnablerun() 메서드 외부의 StatusBarHeight에 액세스하려면 어떻게해야합니까?

@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()에 코드를 직접 입력하면 이 표시되고 이 반환됩니다. 아직 UI가 렌더링되지 않았으므로
도움을 주시면 감사하겠습니다. 당신이에서 onCreate() 메소드에서 액세스하려는 경우

답변

3

귀하의 변수 StatusBarHeight의 범위 내에서 블록의 로컬, 당신은 당신은 선언 할 필요가

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); 
      } 
     }); 
} 
+0

멋진 아이디어! –

+0

'private static'은 클래스 변수 선언입니다. 그것은 여기에 정말로 신중하지 않습니다. (좋은 아이디어도 아닙니다.) OP는 멤버 변수 만 있으면됩니다. –

+0

실행 가능한 인터페이스 또는 스레드 클래스를 처리 할 때 정적 선언이 필요합니다 – Lucifer

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); 
     } 
    }); 
} 

참고 : 내가 StatusBarHeight에서 statusBarHeightTitleBarHeight에서 titleBarHeight에 변수의 이름을 변경했습니다. 변수에는 초기 소문자 이름을 사용해야합니다. Java 명명 규칙은 클래스 이름에 초기 소문자 변수 이름과 초기 대문자를 사용합니다.

관련 문제