Android 气泡框代码
时间: 2023-06-10 07:08:19 浏览: 150
以下是一个简单的 Android 气泡框代码示例:
首先,创建一个自定义的布局文件 `custom_tooltip.xml`,包含一个 LinearLayout 和一个 TextView:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tooltip_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tooltip_background"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/tooltip_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white" />
</LinearLayout>
```
然后,在你的 Activity 中,使用 PopupWindow 来显示气泡框。示例代码如下:
```
// inflate the custom layout
View tooltipView = LayoutInflater.from(this).inflate(R.layout.custom_tooltip, null);
// set the text of the TextView
TextView tooltipText = tooltipView.findViewById(R.id.tooltip_text);
tooltipText.setText("Hello, world!");
// create a new PopupWindow
PopupWindow tooltip = new PopupWindow(tooltipView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// set the PopupWindow to be focusable
tooltip.setFocusable(true);
// show the PopupWindow at a specified location
int[] location = new int[2];
targetView.getLocationOnScreen(location);
tooltip.showAtLocation(targetView, Gravity.NO_GRAVITY, location[0], location[1] - tooltip.getHeight());
```
这里,`targetView` 是你想要在其上显示气泡框的 View。`location` 数组包含 `targetView` 在屏幕上的 x 和 y 坐标。我们使用 `showAtLocation` 方法来显示气泡框,并将其放置在 `targetView` 的下方。
阅读全文
相关推荐
















