【问题标题】:How to make a horizontal popup?如何制作水平弹出窗口?
【发布时间】:2018-08-23 07:13:31
【问题描述】:

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

我已经写了这段代码:

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

但是弹出窗口是透明的,根据我使用的设备放置得不好,直到我点击两次才会消失。

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

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试。我们无法为您编写完整的解决方案。感谢理解。
  • 是的没问题!

标签: android popup touch


【解决方案1】:

试试这个:

在您的活动布局中添加:

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


            }
        }
    });
}

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

【讨论】:

  • 哦,谢谢,这是个好主意!即使我正在寻找动画弹出窗口
  • 您可以将此方法与动画一起使用,但现在我不在笔记本电脑上,因此无法为您提供帮助。搜索如何使用动画将布局翻译到另一个位置,你应该找到你需要的
  • 好的,感谢您的帮助,我将能够以正确的方式进行搜索
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多