Android开发 ---如何操作资源目录中的资源文件2



一、颜色资源管理

效果图:

  描述:

    1、改变字体的背景颜色

    2、改变字体颜色

    3、改变按钮颜色

    4、图像颜色切换

  操作描述:

    点击(1)中的颜色资源管理,进入(2),点击(2)中的微信字样,会出现(3)的效果

    点击(1)中的颜色资源管理2,进入(4),点击(4)中的菜单按钮,菜单按钮的图片和字体的颜色会随之改变

  

            (1)

         (4)                          (2)                           (3)

1、activity_main.xml

  描述:

    在MainActivity中画出两个按钮“颜色资源管理”和“颜色资源管理2”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn1"android:text="颜色资源管理"android:onClick="test_1"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="颜色资源管理2"android:onClick="test_2"/>
</LinearLayout>

2、MainActivity.java

  描述:

    在MainActivity中点击两个按钮可分别进行页面跳转

package com.example.android_colorresoucesdemo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void test_1(View view){Intent intent = new Intent(this,ColorActivity.class);startActivity(intent);}public void test_2(View view){Intent intent = new Intent(this,WeixinMenuActivity.class);startActivity(intent);}
}

3、activity_color.xml

  描述: 

    第一个TextView中:

      背景引用的是系统颜色

      字体颜色引用的是res目录下values包中color.xml文件中定义的颜色  

    第二个TextView中:

      文本颜色引用的是res目录下values包中color.xml文件中定义的颜色

    第三个TextView中:

      并不是通过xml文件来设置字体颜色的而是通过在Activity中使用代码的方式设置文本颜色

         

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_color"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/showText"android:text="这里是颜色资源的使用"android:textSize="30sp"android:background="@android:color/holo_blue_light"android:textColor="@color/colorAccent"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="navicat颜色"android:textSize="25sp"android:textColor="@color/navicat_color"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/changeColor"android:text="代码设置颜色"android:textSize="25sp"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我像不像微信"android:textColor="@color/btn_text_color"android:textSize="25sp"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn1"android:text="微信"android:textColor="@color/btn_text_color"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn2"android:text="微信"android:textColor="@color/btn_text_color"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn3"android:text="微信"android:textColor="@color/btn_text_color"android:drawableLeft="@drawable/btn_image_menu"/>
</LinearLayout>

4、ColorActivity.java  

  描述:

    通过代码的方式设置文本颜色有两种方式:

        1、通过ARGB的方式设置文本颜色

        2、通过引入资源文件中的资源的方式设置文本颜色

    给按钮设置图标,即如何将图像设置在按钮上。

    

package com.example.android_colorresoucesdemo;import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;public class ColorActivity extends Activity {  //通过代码设置文本颜色private TextView changeColor;private Button btn1,btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_color);changeColor = (TextView)findViewById(R.id.changeColor);     //通过ARGB模式设置文本颜色,参数分别代表,红色值、黄色值、蓝色值、透明度//changeColor.setTextColor(Color.argb(225,225,0,0));     //通过资源引入的方式设置文本颜色     //将id为changeColor的TextView的文本颜色设置为资源文件中定义的颜色changeColor.setTextColor(getResources().getColor(R.color.colorAccent));
     //获取两个按钮btn1 = (Button)findViewById(R.id.btn1);btn2 = (Button)findViewById(R.id.btn2);     //读取资源文件中的图片文件w4.jpg Drawable image = getResources().getDrawable(R.drawable.w4);     //四个参数的含义:x、y、width、heightimage.setBounds(0,0,65,65);     //可以给按钮在上下左右设置图标,如果不想在某个地方显示,则设置为null.但是Drawable必须已经setBounds(Rect),意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。btn1.setCompoundDrawables(image,null,null,null);Drawable image2 = getResources().getDrawable(R.drawable.w5);image2.setBounds(0,0,65,65);btn2.setCompoundDrawables(image2,null,null,null);}
}

5、activity_weixin_menu.xml

  描述:

    1、用了相对布局

    2、相对布局中用了线性水平布局

    3、RadioGroup原本是用来设置单选按钮的,这里则将单选按钮设置成图像按钮,实现了按钮的切换操作

  拓展:   

  1. android:layout_above="@id/xxx"  --将控件置于给定ID控件之上

  2. android:layout_below="@id/xxx"  --将控件置于给定ID控件之下

  3. android:layout_toLeftOf="@id/xxx"  --将控件的右边缘和给定ID控件的左边缘对齐

  4. android:layout_toRightOf="@id/xxx"  --将控件的左边缘和给定ID控件的右边缘对齐

  5. android:layout_alignLeft="@id/xxx"  --将控件的左边缘和给定ID控件的左边缘对齐

  6. android:layout_alignTop="@id/xxx"  --将控件的上边缘和给定ID控件的上边缘对齐

  7. android:layout_alignRight="@id/xxx"  --将控件的右边缘和给定ID控件的右边缘对齐

  8. android:layout_alignBottom="@id/xxx"  --将控件的底边缘和给定ID控件的底边缘对齐

  9. android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐

  10. android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐

  11. android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐

  12. android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐

  13. android:layout_centerInParent="true"  --将控件置于父控件的中心位置

  14. android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置

  15. android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_weixin_menu"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/WeixinMenu"android:orientation="horizontal" <!--线性水平布局-->android:background="@android:color/holo_orange_light"android:layout_alignParentBottom="true" <!--避免弹出输入法扰乱布局-->><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:checkedButton="1"android:layout_marginTop="5dp"android:orientation="horizontal"android:id="@+id/radioGroup"><RadioButtonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rad_1"android:text="微信"android:layout_weight="1"android:button="@null"android:clickable="true"android:gravity="center"android:textStyle="bold"android:textColor="@color/btn_text_color"android:drawableTop="@drawable/btn_image_menu"/><RadioButtonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rad_2"android:text="通讯录"android:layout_weight="1"android:button="@null"android:clickable="true"android:gravity="center"android:textStyle="bold"android:textColor="@color/btn_text_color"android:drawableTop="@drawable/btn_image_menu"/><RadioButtonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rad_3"android:text="发现"android:layout_weight="1"android:button="@null"android:gravity="center"android:textStyle="bold"android:clickable="true"android:textColor="@color/btn_text_color"android:drawableTop="@drawable/btn_image_menu"/><RadioButtonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rad_4"android:text="我"android:layout_weight="1"android:button="@null"android:clickable="true"android:gravity="center"android:textStyle="bold"android:textColor="@color/btn_text_color"android:drawableTop="@drawable/btn_image_menu"/></RadioGroup></LinearLayout>
</RelativeLayout>

6、WeixinMenuActivity.java

  描述:

    1、先获取到按钮

    2、将每个按钮分别传入到setDrawableTop()方法中,

    3、通过getCompoundDrawables()[1]  获取RedioButton上方向的图片

      数组0,1,2,3,对应着左,上,右,下

    4、设置图像的宽和高为70

package com.example.android_colorresoucesdemo;import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.RadioButton;public class WeixinMenuActivity extends Activity {RadioButton rb1,rb2,rb3,rb4;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_weixin_menu);rb1 = (RadioButton)findViewById(R.id.rad_1);rb2 = (RadioButton)findViewById(R.id.rad_2);rb3 = (RadioButton)findViewById(R.id.rad_3);rb4 = (RadioButton)findViewById(R.id.rad_4);//调用下面定义的setDrawableTop()方法setDrawableTop(rb1);setDrawableTop(rb2);setDrawableTop(rb3);setDrawableTop(rb4);}public  void setDrawableTop(RadioButton tv){     //数组0,1,2,3,对应着左,上,右,下Drawable drawable = tv.getCompoundDrawables()[1];drawable.setBounds(0,0,70,70);     //动态的画左上右下四个方向,上面代码drawable.setBounds()初始化了图像的位置,现在将drawable放在第二个参数上,代表放在View的上面的位置上tv.setCompoundDrawables(null,drawable,null,null);}
}

7、在res目录下创建一个color包,color包中创建一个btn_text_color.xml文件

btn_text_color.xml

  描述:

    android:state_checked="false" 表示按钮没有被点击时显示的颜色

    android:state_checked="true" 表示按钮被点击时按钮显示的颜色

   如何引用这个资源呢?

     只需在xml文件对应的按钮中使用 android:textColor="@color/btn_text_color"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="#002200" android:state_checked="false"/><item android:state_checked="true" android:color="@color/navicat_color"/>
</selector>

8、在res目录下的drawable包中创建一个btn_image_menu.xml文件,且在包内放置两张图片

w4.jpg      w5.jpg

btn_image_menu.xml

  描述:

    android:state_checked="false" 表示没有点击这个图像时默认显示的图片就是它

    android:state_checked="true" 表示当点击了这个图像时图像显示的样子

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="false"android:drawable="@drawable/w4"/><item android:state_checked="true" android:drawable="@drawable/w5"/>
</selector>

9、在res目录下的values包中修改color.xml文件和styles.xml文件

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><color name="colorPrimary">#3F51B5</color><color name="colorPrimaryDark">#303F9F</color><color name="colorAccent">#FF4081</color><color name="navicat_color">#63CF10</color>
</resources>

styles.xml

<resources><!-- Base application theme. --><style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"><!-- Customize your theme here. --></style><style name="MyTheme" parent="android:Theme.Holo.Light.DarkActionBar"><item name="android:buttonStyle">@style/MyButtonStyle</item><item name="android:textViewStyle">@style/MyTextViewStyle</item></style><style name="MyButtonStyle" parent="android:Widget.Button"><item name="android:textSize">25dp</item><item name="android:textColor">@color/btn_text_color</item><item name="android:clickable">true</item></style><style name="MyTextViewStyle" parent="android:Widget.TextView"><item name="android:textSize">20dp</item><item name="android:textColor">@color/colorAccent</item></style>
</resources>

//四个参数含义:x、y、width、height

转载于:https://www.cnblogs.com/zn615/p/8184654.html

Android开发 ---如何操作资源目录中的资源文件2相关推荐

  1. Android开发笔记(七十九)资源与权限校验

    硬件资源 因为移动设备的硬件配置各不相同,为了防止使用了不存在的设备资源,所以要对设备的硬件情况进行检查.一般情况下,前置摄像头.部分传感器在低端手机上是没有的,像SD卡也可能因为用户没插卡使得找不到 ...

  2. 在 Java 中,如何批量读取本项目资源目录下的所有文件

    在 Java 中,如何批量读取本项目资源目录下的所有文件 读取资源目录下的指定文件 方法 1:使用 JDK 中原始 API 方法 2:借助 Spring 附录 将 InputStream 转化为 by ...

  3. App inventor访问assets目录中的资源文件的方法

    应用中需要打开一个本地的HTML文件.用AI伴侣调试的时候,上载的HTML文件被保存到手机的调试目录中(android/data/com.wxbit.appinventor.aicompanion3/ ...

  4. 一些游戏表象的东西,游戏界面,游戏目录,目录中的资源样子

    以下仅仅是某个游戏项目里的资源和游戏的描述而已,仅仅是描述而已.  记给自己看的. 1,游戏界面包含了这些内容:主要是一些放置方面的,我也不知道为什么要这样放置. 右下(攻击,跳跃,技能,是否切换目标 ...

  5. Android通知怎么实现?Android开发如何操作相机和相册?

    Android通知怎么实现?Android开发如何操作相机和相册? 前言 八.Android通知怎么实现?Android开发如何操作相机和相册? 8.1 通知介绍 8.2 通知的基本用法 8.3 An ...

  6. Android开发笔记(三十四)Excel文件的读写

    Android中操作Excel文件的场合较少见,主要是一些专业领域导入导出报表时使用,所以处理Excel读写的开源代码也很稀缺.目前读写Excel主要采用开源库jxl,这个是韩国人写的excel操作工 ...

  7. Android开发笔记(一百四十)Word文件的读取与显示

    读取纯文本 现在手机的用途越来越广泛,从原来只有通讯功能的电话,到拍照手机,到上网手机,再到办公手机,可谓是无所不能了.说到办公,除了收发邮件,还有个频繁使用的功能,就是处理word文件.电脑上的of ...

  8. Android开发笔记(七十四)布局文件优化

    include/merge 布局优化中常常用到include/merge标签,include的含义类似C代码中的include,意思是直接把指定布局片段包含进当前的布局文件.include适用于多个布 ...

  9. android开发--mp3播放器项目源代码(xml文件解析,.lrc,.mp3文件下载,同时显示歌词)

    一.mp3播放器源代码 1.MainActivity.java:在此中主要负责播放器首页的功能,包括服务器上的下载列表,和SD卡上已经下载的mp3文件列表package com.wyt.MP3play ...

最新文章

  1. 沈向洋、华刚:读科研论文的三个层次、四个阶段与十个问题
  2. 产品经理必备成长修炼秘籍(深度解析)
  3. Android之获取设备的型号和手机厂商
  4. JDK源码解析之java.util.AbstractCollection
  5. python能查询MySQL视图_python - 在使用Django的视图中,如何从mysql检索数据,并显示它_python_酷徒编程知识库...
  6. python合并word全部_python:怎样合并文档中有重复部分的行?
  7. 服务器sxs文件通用的吗,win10x sources sxs文件有什么作用
  8. 参考AWR中的Instance Activity Statistics 计算IOPS
  9. mysql数据库过滤数据_MySQL数据库常规操作一些简单绕过过滤的方法
  10. 基于公司云平台的素材归档系统(一)
  11. 【记录】搭建本地wordpress全过程
  12. MFC模拟360悬浮窗加速球窗口
  13. 基于LabVIEW 2018开发的自动化测试系统源码,该系统模仿TestStand编写
  14. iOS开发:如何修改app名称
  15. 【Rust日报】 2020-01-10 track_caller 錯誤處理大突破
  16. 你说的每一句我都记着,还带时间呢:简洁版纪念日
  17. 关于海盗分金币问题的讨论(面试题)[]
  18. 公司内网成功实现WSUS在不连外网的条件下更新补丁包!
  19. Failed to load ‘D:\webpack.config.js‘ config Error: Cannot find module ‘webpack//libRequestShortener
  20. jquery入门介绍

热门文章

  1. python中sort和sorted区别_Python中的 sort 和 sorted的用法与区别
  2. finereport字段显示设置_如何在Excel中显示和编辑中文拼音字段
  3. java初始化数据报_java – 如何在Docker中初始化数据库后启动flyway
  4. pod中mysql配置文件修改_通过configmap更新k8s里的mysql配置文件
  5. Spring Boot笔记-404错误统一管理
  6. C++笔记-shared_ptr与weak_ptr需要注意的地方
  7. linux工作笔记-linux之间文件传输图形界面工具gftp
  8. Qt工作笔记-QLineEdit中使用setValidator里面的坑
  9. c语言 隐式声明,关于C#:隐式函数声明和链接
  10. 笔记本计算机死机后如何启动,电脑戴尔死机如何重新启动的解决方法