2013-09-27 238 views
5

Tôi có một ứng dụng mà tôi đặt nền cho hình ảnh từ xml. Tôi nhận được chế độ xem bằng cách gọiThêm lớp phủ nửa trong suốt vào hoạt động backround khi chạy

setContentView(R.) 

. Làm cách nào để đặt lớp phủ nửa trong suốt trên nền này vào thời gian chạy tùy thuộc vào điều kiện. Tôi muốn có một lớp phủ màu đỏ với alpha của nó thiết lập đến 50%.

Tôi đã thử tạo một tệp xml riêng biệt với chế độ xem được sao chép và hình ảnh/lớp phủ khác nhưng lộn xộn như tôi phải làm tăng tất cả các nút/văn bản khi tôi sử dụng chế độ xem mới.

nhờ matt

[edit1]

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/carefreebgscaledlighting" 
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 


    <TextView 
     android:id="@+id/textviewcompanyname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#003F87" /> 

    <TextView 
     android:id="@+id/textViewYouAreSignedIn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_alignParentTop="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#003F87" 
     /> 

      <TextView 
     android:id="@+id/textViewUnsentTransactions" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_below="@id/textViewYouAreSignedIn" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#003F87" 
     /> 

[edit2]

<?xml version="1.0" encoding="utf-8"?> 


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 


    <RelativeLayout 
     android:id="@+id/transparentOverlay" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/red_transparent" > 


<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/carefreebgscaledlighting" 
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 




    <TextView 
     android:id="@+id/textviewcompanyname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#003F87" /> 

    <TextView 
     android:id="@+id/textViewYouAreSignedIn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_alignParentTop="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#003F87" 
     /> 

[EDIT3]

<?xml version="1.0" encoding="utf-8"?> 


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 



<LinearLayout 
    android:id="@+id/ll1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/carefreebgscaledlighting" 
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 




    <TextView 
     android:id="@+id/textviewcompanyname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#003F87" /> 

.

if(status.equalsIgnoreCase(IN)){ 

       youAreSignedInAt.setText("You are signed in at " + name); 
       LinearLayout layout =(LinearLayout)findViewById(R.id.ll1); 
       layout.setBackgroundResource(R.drawable.carefreebgscaledlightingred); 
      }else{ 

       youAreSignedInAt.setText("You are not signed in"); 
       LinearLayout layout =(LinearLayout)findViewById(R.id.ll1); 
       layout.setBackgroundResource(R.drawable.carefreebgscaledlighting); 
      } 
+0

Nếu bạn biết mức độ mờ đục mong muốn, tại sao không phải là PNG? Đây sẽ chỉ là một hình ảnh bạn có thể thêm vào bố cục, bất cứ khi nào cần thiết. – Prmths

Trả lời

17

Off đỉnh đầu của tôi, bạn có thể khai báo màu nền bao gồm cả thành phần alpha trong một tập tin tài nguyên (tức là colors.xml)

<color name="red_transparent">#66FF0000</color> 

Và sau đó đặt một cái nhìn (tức RelativeLayout), trên phần còn lại của chế độ xem.-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <!-- All your views --> 

    <!-- Transparent layer --> 
    <RelativeLayout 
     android:id="@+id/transparentOverlay" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/red_transparent" /> 

</RelativeLayout> 

Tất cả những gì bạn cần làm là thay đổi mức hiển thị của R.id.transparentĐược phát khi bạn muốn.

+0

hi tôi đã đăng xml của mình. như bạn có thể thấy tôi có một bộ có thể vẽ làm nền. là nó có thể đặt một chút màu đỏ trên này drawable tại thời gian chạy. đó là mã của bạn? cảm ơn – turtleboy

+0

Nếu bạn muốn lớp phủ màu đỏ ở trên đầu mọi thứ, kể cả bạn có thể vẽ được, có, đó là những gì được cho là để làm đoạn trích của tôi. Nếu bạn chỉ muốn áp dụng một bộ lọc màu đỏ cho bạn có thể vẽ được, đó là một câu chuyện hoàn toàn khác. – ssantos

+0

Tuy nhiên, nếu bạn muốn lớp phủ màu đỏ chỉ trên bản vẽ, bạn có thể đặt bố cục @ + id/transparentOverlay ngay phía trên chế độ xem nếu có thể vẽ được. – ssantos