我正在尝试使用数据绑定将drawable资源ID设置为ImageView的android:src
这是我的对象:
public class Recipe implements Parcelable {
public final int imageResource; // resource ID (e.g. R.drawable.some_image)
public final String title;
// ...
public Recipe(int imageResource, String title /* ... */) {
this.imageResource = imageResource;
this.title = title;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="recipe"
type="com.example.android.fivewaystocookeggs.Recipe" />
</data>
<!-- ... -->
<ImageView
android:id="@+id/recipe_image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@{recipe.imageResource}" />
<!-- ... -->
</layout>
Run Code Online (Sandbox Code Playgroud)
最后,活动类:
// ...
public class RecipeActivity extends AppCompatActivity {
public static …
Run Code Online (Sandbox Code Playgroud) 我想为toString
方法创建IntelliJ Idea模板String.format
而不是连接StringBuffer
,等等.
例如,我有以下对象:
public class Foo {
private int id;
private String name;
private List<String> values;
}
Run Code Online (Sandbox Code Playgroud)
如果我toString
默认为所有字段生成,则会生成:
@Override
public String toString() {
return "Foo{" +
"id=" + id +
", name='" + name + '\'' +
", values=" + values +
'}';
}
Run Code Online (Sandbox Code Playgroud)
但我想生成以下内容:
@Override
public String toString() {
return String.format("Foo(id=%d, name=%s, values=%s)", id, name, values);
}
Run Code Online (Sandbox Code Playgroud) strings.xml中
<string name="my_string">Showing your number: %1$s</string>
Run Code Online (Sandbox Code Playgroud)
ActivityExt.kt
fun Activity.showToast(textResId: Int, vararg formatArgs: String) {
val text = getString(textResId, formatArgs)
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.kt
val number = 11
showToast(R.string.my_string, number.toString())
Run Code Online (Sandbox Code Playgroud)
带有以下文字的吐司显示:
Showing your number: [Ljava.lang.String;@2cfa3b]
Run Code Online (Sandbox Code Playgroud)
为什么会这样?