2012-05-07 35 views
13

Tôi muốn có hoạt động (2) với khía cạnh mờ hơn hoạt động khác (1), căn chỉnh ở đầu màn hình (4).Hoạt động mờ làm đầy toàn bộ màn hình

enter image description here

Tôi đã thử gán các chủ đề này để hoạt động số 2:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> 
    <item name="android:windowBackground">@android:color/black</item> 
</style> 

<style name="CustomTheme"> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:background">@android:color/transparent</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowNoTitle">true</item> 
</style>  

Nhưng kết quả luôn 3.

là Nếu tôi đặt <item name="android:windowIsFloating">false</item> trong CustomTheme kết quả là 2.

Ai có thể cho tôi biết làm cách nào tôi có thể nhận được 4? Cảm ơn!

UPDATE: Đây là hoạt động của tôi 2 bố trí:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" android:background="#0000"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="#FFFFFF"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Menu" android:layout_centerHorizontal="true"/> 

    </RelativeLayout> 

</RelativeLayout> 
+0

http: // stackoverflow.com/questions/36305283/cách thực hiện-mờ-hoạt động – Mangesh

Trả lời

19

Cuối cùng, chủ đề này đã làm việc để có được một kết quả như số hình ảnh 4:

<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent"> 
     <item name="android:backgroundDimEnabled">true</item> 
     <item name="android:backgroundDimAmount">0.5</item> 
     <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
     <item name="android:background">@android:color/transparent</item> 
</style> 

Trong bố trí hoạt động 2 của tôi, tôi có thể eihter thiết android:background="@android:color/transparent" hoặc không đặt bất kỳ giá trị nào cả để làm cho nó làm việc .

Nhờ MikeIsrael và Veer vì sự giúp đỡ của họ.

+0

Tôi chỉ làm những gì bạn đã viết và nó không hoạt động. Tôi có một phong cách như người cuối cùng của bạn, một người trong câu trả lời. 2 hoạt động, và một cái gì đó là mất tích vì nó không phải là minh bạch: ( – Taochok

3

Đầu tiên có hoạt động Theme Transparent:

<style name="Theme.Transparent" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:backgroundDimEnabled">false</item> 
</style> 

Gán các chủ đề trong suốt đối với hoạt động của bạn trong Manifest:

<activity android:name=".MyActivity" 
       android:label="@string/app_name" 
       android:theme="@style/Theme.Transparent"/> 

Tạo bố cục theo yêu cầu của bạn ement và đặt chế độ xem nội dung hoạt động của bạn thành bố cục đó.

Hãy thử sau bố trí:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/transparent"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" 
     android:layout_alignParentTop="true"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Menu" android:layout_centerHorizontal="true"/> 

    </RelativeLayout> 

</RelativeLayout> 

Hy vọng nó sẽ là hữu ích.

+0

Nó không hoạt động. Tôi nhận được nút Menu ở giữa (như hình ảnh số 3) mà không có hiệu ứng mờ. –

+0

bạn có thể đăng xml bố cục của bạn có nút menu không? – Veer

+0

Thử bố cục được thêm vào câu trả lời của tôi – Veer

0

Tôi không chắc chắn làm thế nào để làm điều đó thông qua xml, nhưng điều này nên làm việc lập trình, hãy thử thêm này vào hoạt động của bạn (có thể để xem chính của hoạt động).

//grab layout params 
WindowManager.LayoutParams lp = this.getWindow().getAttributes(); 

//remove the dim effect 
lp.dimAmount = 0; 
+0

Tôi đã thử nó mà không có Theme áp dụng cho hoạt động của tôi, và tôi nhận được kết quả tương tự như trong hình ảnh số 2. Áp dụng với CustomTheme của tôi, tôi đã nhận số 3 mà không có hiệu ứng mờ. –

+0

@Xavi hmmm, có lẽ mờ nhạt không phải từ hoạt động mà là bố cục chính hay gì đó. Còn thử gọi getWindow() thì sao? Có thể thử thêm một id vào realtivelayout rồi grad bố trí bởi findViewBy và sau đó gọi lấy cửa sổ trên layout. Bất cứ thay đổi nào? – MikeIsrael

10

Tôi đã đọc các giải pháp khác, nhưng đây là giải pháp của tôi:

style.xml

<resources> 
<style name="mytransparent.windowNoTitle" parent="android:Theme.Holo.Light"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:background">@android:color/transparent</item> 
    <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> 
</style> 
<style name="mytransparent.windowTitle" parent="android:Theme.Holo.Light"> 
    <item name="android:background">@android:color/transparent</item> 
    <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> 
</style> 

AndroidManifest.xml

<activity 
    android:name=".LoginActivity" 
    android:theme="@style/mytransparent.windowTitle" 
    android:configChanges="orientation" 
    android:label="@string/title_activity_login" 
    android:screenOrientation="portrait" ></activity> 
5

Nếu bạn sử dụng AppCompatActivity sau đó bạn nên sử dụng làm phụ huynh Theme.AppCompat nếu không thì ion có thể treo hoặc lỗi với lỗi (java.lang.RuntimeException: Unable to start activity ComponentInfo ... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.).

<style name="URTransparent" parent="Theme.AppCompat"> 
    // Copied from Theme.Translucent 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:colorBackgroundCacheHint">@null</item> 
    <item name="android:windowIsTranslucent">true</item> 

    <item name="android:backgroundDimEnabled">true</item> 
    <item name="android:backgroundDimAmount">0.5</item> 
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
    <item name="android:background">@android:color/transparent</item> 
</style> 
+0

Điểm tốt, cảm ơn cho người đứng đầu lên. Điều này làm việc cho tôi. – Alix

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