简体   繁体   中英

How to make a horizontal popup?

I don't know how to do a horizontal pop up in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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