2011-10-25 21 views
18

windowIsFloating trong khi cửa hàng dừng chân tuyệt vời để tạo Dialog Giao diện người dùng theo kiểu có nhiều --- lỗi --- quirks.Cách tạo hoạt động trong suốt KHÔNG CÓ cửa sổĐảm bảo

Cái tôi đang chiến đấu ngay bây giờ là nó đã gán chiều rộng/chiều cao của tổ tiên hàng đầu là "wrap_content "chứ không phải chiều rộng/chiều cao của màn hình. Điều này có nghĩa là thiết kế giao diện người dùng thông thường sử dụng "match_parents" sẽ đẩy lên trên .. để trở thành "wrap_content" lần Bad

vì vậy, những gì tôi thực sự muốn là tạo ra một hoạt động, và có một bố cục như vậy:

<LinearLayout android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:orientation="vertical" 
       android:id="@+id/containerPageConatiner" 
       android:background="@drawable/windowBackground"> 
    <View   android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1"/> 
    <FrameLayout android:id="@+id/singlePane" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_gravity="center_horizontal|center_vertical" 
        android:padding="10dp"/>  
    <View   android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1"/>       
</LinearLayout> 

nào tạo ra một giao diện người dùng cho thấy một cửa sổ duy nhất (@ id/singlePane) ontop của Hoạt động gọi là nó.

Có ai có vừa phải bộ kiểu cần thiết để tạo Hoạt động nền trong suốt không?

Trả lời

29

Nhờ @PolamReddy người huých nhẹ tôi về phía câu trả lời tôi muốn:

Chủ đề Theme.Translucent.NoTitleBar.Fullscreen và tổ tiên của nó chứa tất cả các thuộc tính bạn cần để tạo ra một cửa sổ mờ. Để có được tất cả mọi thứ ngoài windowIsFloating tôi đã đi qua tổ tiên ngăn xếp và lấy ra toàn bộ các thuộc tính:

<style name="Theme.CustomTheme.TransparentActivity"> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:colorBackgroundCacheHint">@null</item> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowAnimationStyle">@android:style/Animation</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowFullscreen">true</item> 
</style> 

phong cách này phải được giao cho Activity trong AndroidManifest.xml chứ không phải là quan điểm gốc của một bố cục.

6

sử dụng như thế này cho các hoạt động trong file manifest (chủ đề thể hiện minh bạch của hoạt động đó.)

<activity android:name=".Settings"  
      android:theme="@android:style/Theme.Translucent.NoTitleBar"/> 
0

Câu trả lời được chấp nhận hoạt động tốt, cho đến khi tôi cần có EditText bên trong cửa sổ kiểu hộp thoại, vì bàn phím IME mềm sẽ không đẩy "hộp thoại" lên mà không có windowIsFloating. Vì vậy, tôi phải giải quyết vấn đề 'lỗi'.

Giống như trường hợp trong câu hỏi, tôi cần đặt chiều rộng là "match_parent" trong khi chiều cao có thể giữ nguyên là "wrap_content". Sử dụng windowIsFloating, tôi sẽ thiết lập một ViewTreeObserver, di chuyển lên cây xem trong khi bố trí và buộc cửa sổ nổi đến độ rộng mong muốn. Cách tiếp cận như sau (theo onCreate() của hoạt động) -

setContentView(contentView); 

// set up the ViewTreeObserver 
ViewTreeObserver vto = contentView.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    int displayWidth = -1; 
    DisplayMetrics metrics = null; 
    View containerView = null; 

    @Override 
    public void onGlobalLayout() {    
     if (containerView == null) { 
      // traverse up the view tree 
      ViewParent v = contentView.getParent(); 
      containerView = contentView; 
      while ((v != null) && (v instanceof View)) { 
       containerView = (View) v; 
       v = v.getParent(); 
      } 
     } 
     if (metrics == null) { 
      metrics = new DisplayMetrics(); 
     } 
     Display display = getWindowManager().getDefaultDisplay(); 
     display.getMetrics(metrics); 
     if (displayWidth != metrics.widthPixels) { 
      displayWidth = metrics.widthPixels; 
      WindowManager.LayoutParams params = 
        (WindowManager.LayoutParams) containerView.getLayoutParams(); 
      // set the width to the available width to emulate match_parent 
      params.width = displayWidth; 
      // windowIsFloating may also dim the background 
      // do this if dimming is not desired 
      params.dimAmount = 0f; 
      containerView.setLayoutParams(params); 
      getWindowManager().updateViewLayout(containerView, params); 
     } 
    } 
}); 

này hoạt động lên đến Android 7 cho đến nay (ngay cả trong chế độ chia màn hình), nhưng người ta có thể bắt ngoại lệ cast có thể cho đúc để WindowManager.LayoutParams trong trường hợp thay đổi triển khai trong tương lai và thêm removeOnGlobalLayoutListener(); vào một nơi thích hợp.

Các vấn đề liên quan