0

我在单击按钮时以编程方式添加按钮我需要在该按钮位置显示 PopupWindow 当方向改变时该按钮位置也改变..

4

1 回答 1

0

我想告诉你我的想法,可能不是最好的解决方案,只是一个参考。这里的所有来源都是伪代码。

这是父布局,并将按钮和弹出窗口设置在同一位置:


<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <Button android:layout_marginTop="80dp" 
     android:layout_marginLeft="80dp" >

  </Button>

  <com.yourdomain.android.PopupWindow 
      android:layout_marginTop="80dp" 
      android:layout_marginLeft="80dp" 
      android:visibility="GONE">
  </com.yourdomain.android.PopupWindow> 

</RelativeLayout>

这是您的弹出窗口的布局:

 <LinearLayout
            android:id="@+id/linearlayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" >


</LinearLayout>

在这里,您可以创建一个类来绑定到此布局,因此它是您的自定义组件:

public class PopupWindow extends LinearLayout {

    protected Context _context = null;

    public PopupWindow (Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        _context = context;
        setupView(context);
    }

    public PopupWindow (Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        _context = context;
        setupView(context);
    }

        public void setupView (Context context)
        {
           // here to initialize all children views in this layout
        }

        public void show ()
        {
            this.setVisibility (LinearLayout.Visible);
        }

        public void hide ()
        {
            this.setVisibility (LinearLayout.GONE);
        }

}

我希望这有帮助。

于 2012-09-13T12:05:54.347 回答