作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<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
到它。
更新
我正在获取媒体播放器 View ,但是当我将 xml 附加到它时,我没有得到更改,事实上我认为 xml View 不会自行呈现。
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);
}
}
最佳答案
您需要创建从所需小部件继承的类。在你的例子中,这是 RecyclerView。
public class MyCustomRecyclerView extends RecyclerView {
...
}
然后你可以在 xml 中使用它,如下所示:
<com.example.yourapp.MyCustomRecyclerView
...
/>
更新
也许晚了,但看起来我明白你的意思了。所以是的,您可以将自定义小部件类与 xml 关联起来,如下所示:
public class MyCustomRecyclerView extends RecyclerView {
...
private void init(final Context context) {
inflate(context, R.layout.your_custom_recycler_view_layout, this);
...
}
}
关于java - 如何将 xml View 与 java 类相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58777549/
现在我正在尝试实现 flash programming specification对于 PIC32MX。我正在使用 PIC32MX512L 和 PIC32MX512H。 PIC32MX512L最终必须
我是一名优秀的程序员,十分优秀!