如何绑定布局中存在的视图,该布局是使用 ButterKnife 动态添加到父视图的。
我有一个 LinearLayout 说容器。我有一个自定义布局,其中包含两个按钮,将此布局表示为子视图在活动中我添加了子视图成功到父LinearLayout容器.
这就是我膨胀自定义视图并将其添加到 LinearLayout 的方法
bubbleView = inflater.inflate(R.layout.child, null);
systemChatLayoutContainer.addView(bubbleView);
现在我想绑定里面的按钮视图child布局
并添加当按钮出现在child layout.
这是 child.xml,单击按钮时会动态添加到父容器中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/btnCreateAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_selector_green"
android:gravity="center"
android:padding="@dimen/_13sdp"
android:text="Create an account"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/white" />
<TextView
android:id="@+id/btnJstCheckingRate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_selector_blue"
android:gravity="center"
android:padding="@dimen/_13sdp"
android:text="I'm just checking rates"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/white" />
<TextView
android:id="@+id/btnIhaveAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/transparent_btn_selector"
android:gravity="center"
android:padding="@dimen/_20sdp"
android:text="I've got an account"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/white" />
</LinearLayout>
您可以使用子布局中存在的 ButterKnife 来绑定视图ViewHolder
,所以添加内部类BubbleViewHolder
class BubbleViewHolder {
BubbleViewHolder(View view) {
ButterKnife.bind(this, view);
}
@OnClick(R.id.button_id)
void onMyButtonClicked(Button myButton) {
// Do your stuff here
}
}
并构建BubbleViewHolder
充气后
View bubbleView = inflater.inflate(R.layout.child, null);
new BubbleViewHolder(bubbleView);
systemChatLayoutContainer.addView(bubbleView);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)