如何制作水平弹出窗口?

时间:2018-05-04 12:53:53

标签: android popup touch

我不知道如何在Android中进行水平弹出。我想做这样的事情:

SO50175461 Q example

我已经编写了这段代码:

public void onPopUp(View view) {

    LayoutInflater layoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    final View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT, true);

    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);

    popupWindow.setBackgroundDrawable(new BitmapDrawable());

    View parent = view.getRootView();

    popupWindow.showAtLocation(parent, Gravity.CENTER, 100, 50);
}

但是弹出窗口是透明的,根据我使用的设备放置不好,并且在我点击两次之前不会消失。

如果您有任何想法或我做错了什么,请告诉我!

1 个答案:

答案 0 :(得分:0)

试试这个:

在你的活动布局中添加:

<RelativeLayout
    android:layout_width="300dp"
    android:id="@+id/toMove"
    android:layout_marginLeft="-250dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="80dp"
    android:background="#AA000000"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="Text Here"
        android:textColor="#000"
        android:gravity="center"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

然后在活动类中添加:

public boolean hidden=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.toMove).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(hidden){
                int animationpos = 0;
                ((RelativeLayout)findViewById(R.id.toMove)).animate().x(animationpos).setDuration(1000);

                hidden=false;
            }else{
                hidden=true;
                int animationpos = -800;
                ((RelativeLayout)findViewById(R.id.toMove)).animate().x(animationpos).setDuration(1000);


            }
        }
    });
}

不是最好的方法,但它应该完成工作。