Android笔记6---资源文件

本文全面解析了Android应用的各种资源类型,包括字符串、颜色、尺寸、布局、数组、图像资源等,并详细介绍了mipmap与drawable的区别,以及如何在XML和Java代码中使用这些资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[1] 字符串资源

  1. 定义字符串资源
<string>
  1. 使用字符串资源
    A.在XML中使用
android:text="@string/textName";

B.在java中使用

textID.setText(getResource().getString(R.string.textName));

[2] 颜色资源

color定义

  1. 定义颜色资源
<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>
  1. 使用颜色资源
    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:可伸缩像素

  1. 定义尺寸资源
<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>
  1. 使用尺寸资源
    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] 图像资源

  1. 图片资源
  2. 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区别:

mipmap

[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]样式资源

  1. 主题:是设置整个APP或窗口样式
    样式:是设置组件的样式
  2. 设置在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>
  1. List item

使用:

<TextView
	...
	style="@style/text_down"
	/>

[10] 菜单资源

  1. 在res下创建menu目录
  2. 创建menu.xml资源文件
  3. 设置

[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. 为组件注册上下文菜单
  2. 添加上下文 菜单
  3. 指定菜单项被选择时所应做出的处理
// 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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值