安卓编写一个圆角并悬浮的底部弹窗,并设置好内部按钮的点击事件
时间: 2024-10-11 13:17:13 浏览: 43
要在Android上编写一个圆角并且悬浮的底部弹窗,并设置内部按钮的点击事件,你可以使用`androidx.constraintlayout.widget.ConstraintLayout`配合`com.google.android.material.bottomsheet.BottomSheetDialog`。以下是一个简单的例子:
首先,你需要在布局文件(比如`pop_up_dialog.xml`)中定义弹窗样式:
```xml
<com.google.android.material.bottomsheet.BottomSheetDialog
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetDialog"
style="@style/MyBottomSheetDialog">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_rounded_corner_shape"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<!-- 按钮 -->
<Button
android:id="@+id/button"
android:text="点击我"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="?attr/colorPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.bottomsheet.BottomSheetDialog>
```
这里设置了底部圆角形状,需要你自己在`dialog_rounded_corner_shape.xml`中定义:
```xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="48"
android:viewportHeight="48">
<!-- 使用路径描述圆角图形,添加边框圆角 -->
<path
android:pathData="M24,4 C15.9,4 10,9.9 10,20 c0,10.1 5.9,16 14,16 s14-5.9 14-16 C44,9.9 38.1,4 24,4 z M24,34 C17.7,34 12,28.3 12,22 S17.7,14 24,14 S34,20.7 34,22 C34,28.3 28.3,34 24,34 z"
android:fillColor="#fff"/>
</vector>
```
然后,在你的Activity或Fragment中初始化并监听按钮点击事件:
```java
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.DialogFeature;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.lifecycle.LifecycleOwner;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.bottomsheet.BottomSheetDialog;
public class MainActivity extends AppCompatActivity {
private BottomSheetDialog mBottomSheetDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化BottomSheetDialog
mBottomSheetDialog = BottomSheetDialog.create(this, R.style.MyBottomSheetDialog);
mBottomSheetDialog.getContentView().findViewById<Button>(R.id.button)
.setOnClickListener(v -> handleButtonClick());
// 添加圆角和阴影
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(findViewById(ConstraintLayout.class));
constraintSet.setHorizontalRadius(ViewCompat.getMinimumWidth(findViewById(Button.class)) / 2);
constraintSet.setVerticalRadius(ViewCompat.getMinimumHeight(findViewById(Button.class)) / 2);
constraintSet.applyTo(findViewById(ConstraintLayout.class));
// 其他对话框属性可以根据需求调整,例如设置悬浮或半透明
mBottomSheetDialog.getWindow().setFeatureInt(DialogFeature.BORDERLESS_BOTTOM_SHEET, true);
mBottomSheetDialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this, android.R.color.TRANSPARENT));
}
private void handleButtonClick() {
// 点击按钮的逻辑...
}
}
```
阅读全文