简体   繁体   English

如何制作水平弹出窗口?

[英]How to make a horizontal popup?

I don't know how to do a horizontal pop up in Android. 我不知道如何在Android中进行水平弹出。 I want to make something like this: 我想做这样的事情:

SO50175461 Q的例子

I have already written this code: 我已经写了这段代码:

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);
}

but the popup is transparent, not well placed based on the device I use and won't go away until I click twice. 但弹出式窗口是透明的,根据我使用的设备放置不当,并且直到我单击两次后才会消失。

If you have any idea or if I am doing something wrong, tell me! 如果您有任何想法或我做错了什么,请告诉我!

Try this: 尝试这个:

in your activity layout add this: 在您的活动布局中添加以下内容:

<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>

Then in the activity class add this: 然后在活动类中添加以下内容:

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);


            }
        }
    });
}

Not the best way to do it but it should get the job done. 这不是最好的方法,但是应该可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM