单击android中的按钮时禁用背景布局

时间:2014-03-11 05:52:27

标签: android android-layout popupwindow

我的XML中有两个List视图和一个按钮。单击按钮会出现一个弹出框。所以我想在单击按钮时禁用底层布局。我该怎么办?

这是我的xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:background="#25232c"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/show_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1">
    </ListView>

    <ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1">
    </ListView>

</LinearLayout>

这是弹出框的代码

// Get the x and y position after the button is draw on screen

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

       int[] location = new int[2];
       Button button = (Button) findViewById(R.id.show_popup);

       // Get the x, y location and store it in the location[] array
       // location[0] = x, location[1] = y.
       button.getLocationOnScreen(location);

       //Initialize the Point with x, and y positions
       p = new Point();
       p.x = location[0];
       p.y = location[1];
    }

    // The method that displays the popup.
    private void showPopup(final Activity context, Point p)
    {
       int popupWidth = 550;
       int popupHeight = 350;

       // 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.datepicker_popup, viewGroup);

       // 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 = 7;
       int OFFSET_Y = 65;

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

       // Displaying the popup at the specified location, + offsets.
       popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.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()
       {
           /* disable(content_view);*/
         @Override
         public void onClick(View v) 
         {
            popup.dismiss();
         }
       });

这是弹出窗口的自定义布局

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

        <LinearLayout
            android:id="@+id/popup"
            android:layout_width="308dp"
            android:layout_height="224dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:background="@drawable/popup_bg"
            android:orientation="vertical" >

        <Button
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:text="Close" />

        <Button
            android:id="@+id/now"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Now" />

        <Button
            android:id="@+id/done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Done" />

        </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

你可以试试  listView.setVisibility(View.INVISIBLE);listView.setVisibility(View.GONE);

单击按钮时

答案 1 :(得分:0)

按钮内的onClick

LinearLayout linearLayout =  = (LinearLayout ) findViewById(R.id.main_layout);
linearLayout.setEnabled(false);

答案 2 :(得分:0)

启动示例:

final LinearLayout ll = (LinearLayout) findViewById(R.id.main_layout);
Button btn = (Button) findViewById(R.id.show_popup);
btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        ll.setVisibility(View.GONE);
    }
});