gpt4 book ai didi

android - CustomLayout 的 subview 中出现意外的命名空间前缀应用程序

转载 作者:行者123 更新时间:2023-11-29 18:42:28 25 4
gpt4 key购买 nike

CustomLayout 的 subview 中使用属性时出现错误(我定义了自定义 LayoutParams 以允许 subview 使用此属性)

enter image description here

但是,代码仍在RUNNING 并显示正确的值(您可以在下面查看我的代码,当我运行应用程序时,它会在 logcat 中显示“Hello”)

这是我的代码style.xml

<declare-styleable name="CustomRelativeLayout_Layout">
<attr name="title" format="string" />
</declare-styleable>

CustomRelativeLayout 类

public class CustomRelativeLayout extends RelativeLayout {

public CustomRelativeLayout(Context context) {
this(context, null);
}

public CustomRelativeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new CustomLayoutParams(getContext(), attrs);
}

@Override
public void onViewAdded(View child) {
super.onViewAdded(child);
CustomLayoutParams layoutParams = (CustomLayoutParams) child.getLayoutParams();
Log.i("TAG", "title = " + layoutParams.title);
}

final class CustomLayoutParams extends RelativeLayout.LayoutParams {
String title;

CustomLayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray ta =
c.obtainStyledAttributes(attrs, R.styleable.CustomRelativeLayout_Layout);
title = ta.getString(R.styleable.CustomRelativeLayout_Layout_title);
ta.recycle();
}
}
}

当然,我可以通过添加 tools:ignore="MissingPrefix" 来禁用它,但我不太喜欢这样。任何帮助或建议将不胜感激。

这是我的演示 https://github.com/PhanVanLinh/AndroidPassAttributeToChildVIew

最佳答案

经过多次尝试,我找到了解决问题的方法

title 更改为 layout_title 将使错误消失

<declare-styleable name="CustomRelativeLayout_Layout">
<attr name="title" format="string" />
</declare-styleable>

魔法layout_ 因为如果我使用 like 仍然会发生错误title_title_dasdasdsadlo_titletitle_23232

关于android - CustomLayout 的 subview 中出现意外的命名空间前缀应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52808751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com