Android应用的资源
[1] 字符串资源
- 定义字符串资源
<string>
- 使用字符串资源
A.在XML中使用
android:text="@string/textName";
B.在java中使用
textID.setText(getResource().getString(R.string.textName));
[2] 颜色资源
- 定义颜色资源
<color>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="wordcolor">#FFFFFF</color>
<color name="textView1">#BBE24A83</color>
- 使用颜色资源
A.在XML中使用
android:textColor="@color/title";
B.在java中使用
textID.setTextColor(getResource().getColor(R.color.title));
textID.setTextColor(getColor(R.color.title)); //java6.0以后
[3] 尺寸资源
dp:表示设备独立像素,一般用于设置边距,组件大小
sp:可伸缩像素
- 定义尺寸资源
<dimen>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="wordsize">18sp</dimen>
<dimen name="margin">5dp</dimen>
- 使用尺寸资源
A.在XML中使用
android:textSize="@dimen/wordsize";
B.在java中使用
textID.setTextSize(getResource().getDimention(R.dimen.wordsize));
[4] 布局资源
A.在XML中使用
<include layout ="@layout/image"></include>
B.在java中使用
一个布局包含另一个布局的示例
setContentView(R.layout.activity_main);
[5] 数组资源
<string-array name="listitem">
<item>活着就是为了改变世界</item>
<item>尝试很重要</item>
<item>没有失败</item>
</string-array>
A.在XML中使用
<ListView
...
android:entries="@array/listitem"
>
</ListView>
B.在java中使用
String [] arr=getResources().getStringArray(R.array.listitem);
[6] 图像资源
- 图片资源
- StateListDrawable资源
创建edittext_focused.xml文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#f60"></item>
<item android:state_focused="false" android:color="#0f0"></item>
</selector>
使用:
android:textColor="@drawable/edittext_focused"
动态使用:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editText.length() > 0) { // 判断编辑框内输入文字时
button.setBackgroundResource(R.drawable.green); //登录按钮背景色为绿色
button.setEnabled(true); //登录按钮为可用状态
} else { //编辑框内没有文字时
button.setBackgroundResource(R.drawable.green_mint); //登录按钮背景色为浅绿色
button.setEnabled(false); //登录按钮为不可用状态
}
}
[7]mipmap和drawable区别:
[8] 主题资源
创建styles.xml
<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="bgTheme" parent="@style/AppTheme">
<item name="android:windowNoTitle">false</item>
<item name="android:windowBackGround">@drawable/background</item>
</style>
</resources>
使用:
第一种方式:AndroidManifest.xml中设置
android:theme="@style/bgTheme">
第二种 方式:在java中,一定要放在setContentView之前,否则将不起作用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_main);
[9]样式资源
- 主题:是设置整个APP或窗口样式
样式:是设置组件的样式 - 设置在styles.xml中
<style name="black">
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/black</item>
</style>
<style name="text_down" parent="black">
<item name="android:layout_gravity">center_horizontal</item>
</style>
- List item
使用:
<TextView
...
style="@style/text_down"
/>
[10] 菜单资源
- 在res下创建menu目录
- 创建menu.xml资源文件
- 设置
[11] 选项菜单
定义
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.itcast.cn.optionmenu.MainActivity">
<item
android:id="@+id/settings"
android:title="@string/menu_title_settings"></item>
<item
android:id="@+id/regard"
android:title="@string/menu_title_regard"></item>
</menu>
使用
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater =new MenuInflater(this); //实例化一个MenuInflater对象
menuInflater.inflate(R.menu.menu_main,menu); //解析菜单文件
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
Intent intent= new Intent(MainActivity.this,Activity_setting.class);
startActivity(intent);
break;
case R.id.regard:
Intent intent1= new Intent(MainActivity.this,regard.class);
startActivity(intent1);
break;
}
return super.onOptionsItemSelected(item);
}
[12] 上下文菜单
- 为组件注册上下文菜单
- 添加上下文 菜单
- 指定菜单项被选择时所应做出的处理
// 1. 为组件注册上下文菜单
TextView introduce = findViewById(R.id.introduce);
registerForContextMenu(introduce)
// 2. 添加上下文 菜单
@Override
public boolean onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo){
MenuInflater menuInflater =new MenuInflater(this); //实例化一个MenuInflater对象
menuInflater.inflate(R.menu.menu,menu); //解析菜单文件
}
// 3. 指定菜单项被选择时所应做出的处理
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
Intent intent= new Intent(MainActivity.this,Activity_setting.class);
startActivity(intent);
break;
case R.id.regard:
Intent intent1= new Intent(MainActivity.this,regard.class);
startActivity(intent1);
break;
}
return true;
}