2012-12-05 41 views
1

我有一個彈出窗口。我想彈出窗口的LinearLayout的背景變化動態地從android:background="@drawable/popup_bg"android:background="@drawable/popup_bg2"如何動態設置彈出窗口的線性佈局的背景?

這裏是我的代碼:

private void showPopup(final Activity context, int x, int y, String id, String n, String a, String l) { 
    int popupWidth = 200; 
    int popupHeight = 150; 

    // Inflate the popup_layout.xml 
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup); 


    LayoutInflater layoutInflater = (LayoutInflater) context 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup); 

    TextView id1 = (TextView) layout.findViewById(R.id.id); 
    TextView name = (TextView) layout.findViewById(R.id.name); 
    TextView absent = (TextView) layout.findViewById(R.id.absence); 
    TextView late = (TextView) layout.findViewById(R.id.lateness); 

    id1.setText(id); 
    name.setText(n); 
    absent.setText(a); 
    late.setText(l); 

    // Creating the PopupWindow 
    final PopupWindow popup = new PopupWindow(context); 
    popup.setContentView(layout); 
    popup.setWidth(popupWidth); 
    popup.setHeight(popupHeight); 
    popup.setFocusable(true); 

    // Some offset to align the popup a bit to the right, and a bit down, relative to button's position. 
    int OFFSET_X = 30; 
    int OFFSET_Y = 300; 

    // Clear the default translucent background 
    popup.setBackgroundDrawable(new BitmapDrawable()); 

    // Displaying the popup at the specified location, + offsets. 
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, x + OFFSET_X, y +OFFSET_Y); 

    // Getting a reference to Close button, and close the popup when clicked. 
    Button close = (Button) layout.findViewById(R.id.close); 
    close.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     popup.dismiss(); 
    } 
    }); 
} 

popup_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:id="@+id/popup" 
    android:layout_height="wrap_content" 
    android:background="@drawable/popup_bg" 
    android:orientation="vertical" > 
</LinearLayout> 

回答

3

試試這個,

 LinearLayout ll=popup.getContentView().findViewById(R.id.popup); 
    ll.setBackgroundResource(R.drawable.yourImage); 

提供,你這樣做,popup.setContentView(layout);在做上述之前。