【发布时间】:2019-11-09 08:16:17
【问题描述】:
<android.support.v7.widget.RecyclerView
android:id="@+id/menu_list_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
</android.support.v7.widget.RecyclerView>
考虑RecyclerView的例子,我们将上面的代码放在xml中,并像这样在java类中创建它的实例
menuListRV = (RecyclerView)findViewById(R.id.sugg_rv);
在此之后,我们可以随心所欲地控制它。
现在我的问题是如何创建我自己的 xml 标记 <xyz></xyz> 可能具有一些属性并将类 XYZ 关联到它。
更新
我正在获取媒体播放器视图,但是当我将 xml 附加到它时,我没有得到更改,事实上我认为 xml 视图本身没有呈现。
public class ShortMediaPlayerControl extends ViewGroup {
public ShortMediaPlayerControl(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context);
Toast.makeText(context, "Coming 2", Toast.LENGTH_SHORT).show();
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ShortMediaPlayerControl, 0, 0);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
private void inflate(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.short_media_player, this, true);
}
}
【问题讨论】:
-
你应该参考这个链接stacktips.com/tutorials/android/…
-
@Swayangjit 我们不能将 xml 与新类关联吗?
-
是的,你可以拥有