Android View 里面有用的函数

/**
 * Called when the window containing has change its visibility
 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}).  Note
 * that this tells you whether or not your window is being made visible
 * to the window manager; this does <em>not</em> tell you whether or not
 * your window is obscured by other windows on the screen, even if it
 * is itself visible.
 *
 * @param visibility The new visibility of the window.
 */
protected void onWindowVisibilityChanged(@Visibility int visibility)
/**
 * Called when the visibility of the view or an ancestor of the view has
 * changed.
 *
 * @param changedView The view whose visibility changed. May be
 *                    {@code this} or an ancestor view.
 * @param visibility The new visibility, one of {@link #VISIBLE},
 *                   {@link #INVISIBLE} or {@link #GONE}.
 */
protected void onVisibilityChanged(@NonNull View changedView, @Visibility int visibility)
/**
 * Called when the user-visibility of this View is potentially affected by a change
 * to this view itself, an ancestor view or the window this view is attached to.
 *
 * @param isVisible true if this view and all of its ancestors are {@link #VISIBLE}
 *                  and this view's window is also visible
 */
@CallSuper
public void onVisibilityAggregated(boolean isVisible)

 

 

 

 

 

### Android 14 中向 `ExpandableNotificationRow` 添加自定义 ViewAndroid 14 中,为了实现对 `ExpandableNotificationRow` 的扩展并添加自定义视图,通常涉及修改 System UI 组件的通知布局逻辑。具体操作如下: #### 修改通知布局逻辑 通过继承 `ExpandableNotificationRow` 类来创建一个新的类,在其中重写方法以支持新的视图结构[^3]。 ```java public class CustomExpandableNotificationRow extends ExpandableNotificationRow { public CustomExpandableNotificationRow(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); // 初始化自定义组件 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.custom_notification_view, this, false); addView(customView); } } ``` 此代码片段展示了如何通过覆盖 `onFinishInflate()` 方法来自定义 `ExpandableNotificationRow` 并引入额外的视图资源文件 `R.layout.custom_notification_view`。 #### 定义自定义视图 XML 文件 确保在项目的 res/layout 目录下存在名为 `custom_notification_view.xml` 的文件,该文件描述了想要显示的具体界面元素及其属性设置[^2]。 ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 自定义控件 --> <TextView android:id="@+id/widget_title" android:text="Custom Title" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ``` 上述 XML 片段提供了一个简单的垂直线性布局作为例子,实际应用中可以根据需求调整设计。 #### 注册监听器和服务接口 如果需要进一步控制或响应特定事件,则可能还需要注册相应的监听器以及服务接口。这可以通过扩展 `INotificationListener.Stub` 来完成,并在其内部处理各种回调函数。 ```java protected static final class NotificationListenerWrapper extends INotificationListener.Stub { private final WeakReference<Context> mContextRef; public NotificationListenerWrapper(Context context) { mContextRef = new WeakReference<>(context); } @Override public void onNotificationPosted(StatusBarNotification sbn) throws RemoteException { Context context = mContextRef.get(); if (context != null && sbn != null){ // 处理新发布的通知... } } // ...其他必要的覆写的APIs } ``` 这段 Java 代码说明了怎样建立一个用于监控通知变化的服务端点,从而允许应用程序根据实际情况做出反应。 综上所述,在 Android 14 上为 `ExpandableNotificationRow` 添加自定义视图主要依赖于对现有框架的理解和技术细节上的深入挖掘。除了以上提到的技术要点外,还应该关注官方文档和其他权威资料获取最新指导和支持[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值