自定义视图中的行为类似于 tools:text?
Behavior like tools:text in a custom view?
我有一个自定义视图,布局中有两个文本视图。我们称一个 key
另一个 value
.
所以你知道 TextView
是怎么做到的吗?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Preview text shown in the layout editor" />
我想对我的自定义视图做一些类似的事情,例如:
<com.package.whatever.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:key_preview="Preview text for the key"
app:value_preview="Preview text for the value" />
MyCustomView
的构造函数是这样的:
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.my_custom_view, this, true);
key = (TextView) findViewById(R.id.key);
value = (TextView) findViewById(R.id.value);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, 0, 0);
try {
if (isInEditMode()) {
key.setText(a.getText(R.styleable.MyCustomView_key_preview));
value.setText(a.getText(R.styleable.MyCustomView_value_preview));
}
key.setText(a.getText(R.styleable.MyCustomView_key));
value.setText(a.getText(R.styleable.MyCustomView_value));
} finally {
a.recycle();
}
}
和attrs.xml
:
<declare-styleable name="MyCustomView">
<attr name="key" format="string" />
<attr name="key_preview" format="string" />
<attr name="value" format="string" />
<attr name="value_preview" format="string" />
</declare-styleable>
问题 是当我在布局编辑器中查看包含 MyCustomView
的布局时,isInEditMode()
返回 false
。如果我添加 app:key="yay"
它工作正常,但是对于 app:key_preview="nay :("
没有任何显示。
什么给了?
我是个骗子,骗了你们所有人。 isInEditMode()
没有返回 false。
咳咳:
if (isInEditMode()) {
key.setText(a.getText(R.styleable.MyCustomView_key_preview));
value.setText(a.getText(R.styleable.MyCustomView_value_preview));
} else {
key.setText(a.getText(R.styleable.MyCustomView_key));
value.setText(a.getText(R.styleable.MyCustomView_value));
}
事实证明,我最初实际上是将密钥设置为 app:key_preview
的值,但是随后它被 a.getText(R.styleable.MyCustomView_key)
返回的 null
覆盖。
呜呜呜。
我有一个自定义视图,布局中有两个文本视图。我们称一个 key
另一个 value
.
所以你知道 TextView
是怎么做到的吗?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Preview text shown in the layout editor" />
我想对我的自定义视图做一些类似的事情,例如:
<com.package.whatever.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:key_preview="Preview text for the key"
app:value_preview="Preview text for the value" />
MyCustomView
的构造函数是这样的:
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.my_custom_view, this, true);
key = (TextView) findViewById(R.id.key);
value = (TextView) findViewById(R.id.value);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, 0, 0);
try {
if (isInEditMode()) {
key.setText(a.getText(R.styleable.MyCustomView_key_preview));
value.setText(a.getText(R.styleable.MyCustomView_value_preview));
}
key.setText(a.getText(R.styleable.MyCustomView_key));
value.setText(a.getText(R.styleable.MyCustomView_value));
} finally {
a.recycle();
}
}
和attrs.xml
:
<declare-styleable name="MyCustomView">
<attr name="key" format="string" />
<attr name="key_preview" format="string" />
<attr name="value" format="string" />
<attr name="value_preview" format="string" />
</declare-styleable>
问题 是当我在布局编辑器中查看包含 MyCustomView
的布局时,isInEditMode()
返回 false
。如果我添加 app:key="yay"
它工作正常,但是对于 app:key_preview="nay :("
没有任何显示。
什么给了?
我是个骗子,骗了你们所有人。 isInEditMode()
没有返回 false。
咳咳:
if (isInEditMode()) {
key.setText(a.getText(R.styleable.MyCustomView_key_preview));
value.setText(a.getText(R.styleable.MyCustomView_value_preview));
} else {
key.setText(a.getText(R.styleable.MyCustomView_key));
value.setText(a.getText(R.styleable.MyCustomView_value));
}
事实证明,我最初实际上是将密钥设置为 app:key_preview
的值,但是随后它被 a.getText(R.styleable.MyCustomView_key)
返回的 null
覆盖。
呜呜呜。