Resources资源目录存放应用用到的各类素材:图片、图标、布局、字符串文本、样式主题等。
1. values/string.xml 字符串资源
存放项目文本字符串,方便统一修改、多语言适配。
<resources>
<string name="app_name">GUIForm</string>
<string name="action_settings">Settings</string>
</resources>Code language: HTML, XML (xml)
- 在XML布局引用:
@string/app_name - C#代码引用:
Resources.GetString(Resource.String.app_name)
2. Style.xml 样式与主题
style用来统一定义控件、Activity的外观样式,主题作用于整个页面/Activity。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>Code language: HTML, XML (xml)
自定义启动页主题
实现无标题、窗口设置背景图片,用作Splash闪屏页面。
<style name="mySplashScreen.Theme" parent="android:Theme">
<item name="android:windowBackground">@drawable/mySplashPicture</item>
<item name="android:windowNoTitle">true</item>
</style>Code language: HTML, XML (xml)
在Activity特性中指定主题(Xamarin.Android)
[Activity(Label = "@string/app_name",
Theme = "@style/mySplashScreen.Theme",
MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
}
- 资源全部放在
Resources文件夹,不同子文件夹存放不同类型资源values/string.xml管理文字,便于后期改文字、做多语言Style.xml写样式主题;parent继承已有主题,只改写需要改动的项- 可以在
Activity特性上指定Theme,给单独页面切换主题;@style/xxx引用自定义样式- drawable目录存放图片、drawable图形,
@drawable/文件名引用图片资源
Previous: 表格布局 TableLayout
Next: 为应用设置 ICON 图标