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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="颜色资源管理"
android:onClick="test_1"
/>
<Button
android: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 {
@Override
protected 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"
>
<TextView
android: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"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="navicat颜色"
android:textSize="25sp"
android:textColor="@color/navicat_color"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/changeColor"
android:text="代码设置颜色"
android:textSize="25sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我像不像微信"
android:textColor="@color/btn_text_color"
android:textSize="25sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="微信"
android:textColor="@color/btn_text_color"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:text="微信"
android:textColor="@color/btn_text_color"
/>
<Button
android: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;
@Override
protected 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、height
image.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"
>
<LinearLayout
android: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" <!--避免弹出输入法扰乱布局-->
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="1"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:id="@+id/radioGroup"
>
<RadioButton
android: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"
/>
<RadioButton
android: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"
/>
<RadioButton
android: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"
/>
<RadioButton
android: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;
@Override
protected 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

Android开发 ---如何操作资源目录中的资源文件2的更多相关文章

  1. Android开发---如何操作资源目录中的资源文件4 ---访问xml的配置资源文件的内容

    Android开发---如何操作资源目录中的资源文件4 XML,位于res/xml/,这些静态的XML文件用于保存程序的数据和结构. XmlPullParser可以用于解释xml文件 效果图: 描述: ...

  2. Android开发---如何操作资源目录中的资源文件3--圆角边框、背景颜色渐变效果、边框颜色

    Android开发---如何操作资源目录中的资源文件3 效果图 1.圆角边框 2.背景颜色渐变效果 1.activity_main.xml 描述: 定义了一个shape资源管理按钮 <?xml ...

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

    效果图: 1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...

  4. Android开发 ---如何操作资源目录中的资源文件5 ---Raw资源管理与国际化

    效果图: 1.activity_main.xml 描述: 定义两个按钮,一个是Raw资源管理,一个是处理国际化语言,其中i18n表示简体中文 <?xml version="1.0&qu ...

  5. android开发之-查看、编辑手机sqlite数据库文件-实测

    效果图: 1.开始——运行——输入cmd ,输入adb shell,错误:一是“adb不是内部命令或外部命令,也不是可运行的程序或批处理文件”,二是“error:device not found”. ...

  6. 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world

    一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...

  7. 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来

    /*4.创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来*/ #import <Foundation/Foundation.h>#defin ...

  8. python实现在目录中查找指定文件的方法

    python实现在目录中查找指定文件的方法 本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 代码如下: import os from ...

  9. 如何查找一个目录中所有c文件的总行数

    如何查找一个目录中所有c文件的行数 面试题问到了一题,如何统计wc文件夹下所有文件的行数,包括了子目录. 最后在 https://blog.csdn.net/a_ran/article/details ...

随机推荐

  1. R基本图形示例及代码(持续收集)

    分布图 hist(MetaData$genes, breaks = 100, main = "Gene number distribution", xlab = "Gen ...

  2. 【debug、info、warn、error】四者之间的区别与用法

    debug:需要在调试过程中输出的信息,但发布后是不需要的(当然发布后,也是看不到的) info:需要持续输出的信息(无论调试还是发布状态) warn:警告级别的信息(不严重) error:错误信息( ...

  3. H5微信页面开发 IOS系统 input输入框失去焦点,软键盘关闭后,被撑起的页面无法回退到原来正常的位置,导致弹框里的按钮响应区域错位

    H5微信页面开发,软键盘弹起后,若原输入框被遮挡,页面整体将会上移,然而当输入框失焦,软键盘收起后,页面未恢复,导致弹框里的按钮响应区域错位. 解决方案:给输入框(或select选择框)添加失去焦点的 ...

  4. laravel安装Excel安装不上

    1.生明版本号 composer require maatwebsite/excel 2.1我的PHP是7.0安装Excel得2.1 2.在composer.json中加入 "maatweb ...

  5. 伪分布式&&完全分布式&&高可用(zookeeper)的配置文件内容

    [伪分布式] ①[core-site.xml] <configuration> <property> <name>fs.defaultFS</name> ...

  6. poj-2888-矩阵+polya

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 6195   Accepted: 1969 D ...

  7. linux创建定时任务,定时执行sql

    终于弄清楚一个问题了.linux创建定时任务,定时执行sql,其中分为两个case. case-1 sql语句较少,因此直接在 shell脚本中 写sql语句.如下: [oracle@Oracle11 ...

  8. 关于 EF 对象的创建问题

    在开发过程中,项目往往被划分为多层,而一个请求过来往往是从表示层开始一层一层向下调用,那么如果我们在不同的层中都使用到了 EF 上下文对象,而 有好几层都这么创建一个 EF 对象然后对其进行操作,那么 ...

  9. 三、持久层框架(Hibernate)

    一.Hibernate处理关系 关系主要有三种:1.多对一 2.一对多 3.多对多 1.多对一 一个Product对应一个Category,一个Category对应多个Product(一个产品对应一个 ...

  10. ActiveMQ 配置jdbc主从

    使用 jdbc 方式配置主从模式,持久化消息存放在数据库中. 在同一时刻,只有一个 master broker,master 接受客户端的连接,slave 不接受连接.当 master 因为关机而下线 ...