import androidx.annotation.StringRes
data class Question(@StringRes val textResId:Int, val answer:Boolean)
这里@StringRes 注解可以不加,但最好加上,原因有两个:
- 首先,Android Studio内置有Lint代码检查器,有了该注解,它在编译时就知道构造函数会提供有效的资源ID。这样一来,构造函数使用无效资源ID的情况(比如提供的资源ID指向非String类型资源)就能避免,从而阻止了应用的运行时崩溃。
- 其次,注解可以方便其他开发人员阅读和理解你的代码。
开发预览,但设备运行时,系统忽略
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:gravity="center"
tools:text="@string/question_australia"/>
利用tools
可以实现
字符串-单引号-需转义
<string name="question_asia">Lake Baikal is the world's
oldest and deepest freshwater lake.</string>
<string name="question_asia">Lake Baikal is the world\'s
oldest and deepest freshwater lake.</string>
防止数组索引越界
currentIndex = (currentIndex + 1) % questionBank.size
给按钮周围添加图片
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:textColor="@color/button_text_selector"
style="?android:attr/buttonBarButtonStyle"
android:drawableEnd="@drawable/arrow_right"
android:drawablePadding="4dp"
android:text="@string/next_button"/>
android:drawableEnd
android:drawablePadding
为视力障碍提供方便的属性
android:contentDescription
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_left"
style="?android:attr/buttonBarButtonStyle"
android:contentDescription="@string/image_description_prev" />