Android开发 ---如何操作资源目录中的资源文件2
Android開發(fā) ---如何操作資源目錄中的資源文件2
??
一、顏色資源管理
效果圖:
描述:
1、改變字體的背景顏色
2、改變字體顏色
3、改變按鈕顏色
4、圖像顏色切換
操作描述:
點(diǎn)擊(1)中的顏色資源管理,進(jìn)入(2),點(diǎn)擊(2)中的微信字樣,會(huì)出現(xiàn)(3)的效果
點(diǎn)擊(1)中的顏色資源管理2,進(jìn)入(4),點(diǎn)擊(4)中的菜單按鈕,菜單按鈕的圖片和字體的顏色會(huì)隨之改變
(1)
?
(4) (2) (3)
?
1、activity_main.xml
描述:
在MainActivity中畫出兩個(gè)按鈕“顏色資源管理”和“顏色資源管理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中點(diǎn)擊兩個(gè)按鈕可分別進(jìn)行頁(yè)面跳轉(zhuǎn)
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
描述:
第一個(gè)TextView中:
背景引用的是系統(tǒng)顏色
字體顏色引用的是res目錄下values包中color.xml文件中定義的顏色
第二個(gè)TextView中:
文本顏色引用的是res目錄下values包中color.xml文件中定義的顏色
第三個(gè)TextView中:
并不是通過(guò)xml文件來(lái)設(shè)置字體顏色的而是通過(guò)在Activity中使用代碼的方式設(shè)置文本顏色
<?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="代碼設(shè)置顏色"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
描述:
通過(guò)代碼的方式設(shè)置文本顏色有兩種方式:
1、通過(guò)ARGB的方式設(shè)置文本顏色
2、通過(guò)引入資源文件中的資源的方式設(shè)置文本顏色
給按鈕設(shè)置圖標(biāo),即如何將圖像設(shè)置在按鈕上。
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 {
//通過(guò)代碼設(shè)置文本顏色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);
//通過(guò)ARGB模式設(shè)置文本顏色,參數(shù)分別代表,紅色值、黃色值、藍(lán)色值、透明度//changeColor.setTextColor(Color.argb(225,225,0,0));
//通過(guò)資源引入的方式設(shè)置文本顏色
//將id為changeColor的TextView的文本顏色設(shè)置為資源文件中定義的顏色changeColor.setTextColor(getResources().getColor(R.color.colorAccent));
//獲取兩個(gè)按鈕btn1 = (Button)findViewById(R.id.btn1);btn2 = (Button)findViewById(R.id.btn2);
//讀取資源文件中的圖片文件w4.jpg Drawable image = getResources().getDrawable(R.drawable.w4);
//四個(gè)參數(shù)的含義:x、y、width、heightimage.setBounds(0,0,65,65);
//可以給按鈕在上下左右設(shè)置圖標(biāo),如果不想在某個(gè)地方顯示,則設(shè)置為null.但是Drawable必須已經(jīng)setBounds(Rect),意思是你要添加的資源必須已經(jīng)設(shè)置過(guò)初始位置、寬和高等信息。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、用了相對(duì)布局
2、相對(duì)布局中用了線性水平布局
3、RadioGroup原本是用來(lái)設(shè)置單選按鈕的,這里則將單選按鈕設(shè)置成圖像按鈕,實(shí)現(xiàn)了按鈕的切換操作
拓展:
android:layout_above="@id/xxx"? --將控件置于給定ID控件之上
android:layout_below="@id/xxx"? --將控件置于給定ID控件之下
android:layout_toLeftOf="@id/xxx"? --將控件的右邊緣和給定ID控件的左邊緣對(duì)齊
android:layout_toRightOf="@id/xxx"? --將控件的左邊緣和給定ID控件的右邊緣對(duì)齊
android:layout_alignLeft="@id/xxx"? --將控件的左邊緣和給定ID控件的左邊緣對(duì)齊
android:layout_alignTop="@id/xxx"? --將控件的上邊緣和給定ID控件的上邊緣對(duì)齊
android:layout_alignRight="@id/xxx"? --將控件的右邊緣和給定ID控件的右邊緣對(duì)齊
android:layout_alignBottom="@id/xxx"? --將控件的底邊緣和給定ID控件的底邊緣對(duì)齊
android:layout_alignParentLeft="true"? --將控件的左邊緣和父控件的左邊緣對(duì)齊
android:layout_alignParentTop="true"? --將控件的上邊緣和父控件的上邊緣對(duì)齊
android:layout_alignParentRight="true"? --將控件的右邊緣和父控件的右邊緣對(duì)齊
android:layout_alignParentBottom="true" --將控件的底邊緣和父控件的底邊緣對(duì)齊
android:layout_centerInParent="true"? --將控件置于父控件的中心位置
android:layout_centerHorizontal="true"? --將控件置于水平方向的中心位置
android:layout_centerVertical="true"? --將控件置于垂直方向的中心位置
6、WeixinMenuActivity.java
描述:
1、先獲取到按鈕
2、將每個(gè)按鈕分別傳入到setDrawableTop()方法中,
3、通過(guò)getCompoundDrawables()[1]? 獲取RedioButton上方向的圖片
數(shù)組0,1,2,3,對(duì)應(yīng)著左,上,右,下
4、設(shè)置圖像的寬和高為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);//調(diào)用下面定義的setDrawableTop()方法setDrawableTop(rb1);setDrawableTop(rb2);setDrawableTop(rb3);setDrawableTop(rb4);}public void setDrawableTop(RadioButton tv){//數(shù)組0,1,2,3,對(duì)應(yīng)著左,上,右,下Drawable drawable = tv.getCompoundDrawables()[1];drawable.setBounds(0,0,70,70);
//動(dòng)態(tài)的畫左上右下四個(gè)方向,上面代碼drawable.setBounds()初始化了圖像的位置,現(xiàn)在將drawable放在第二個(gè)參數(shù)上,代表放在View的上面的位置上tv.setCompoundDrawables(null,drawable,null,null);} }
7、在res目錄下創(chuàng)建一個(gè)color包,color包中創(chuàng)建一個(gè)btn_text_color.xml文件
?
btn_text_color.xml
描述:
android:state_checked="false" 表示按鈕沒有被點(diǎn)擊時(shí)顯示的顏色
android:state_checked="true" 表示按鈕被點(diǎn)擊時(shí)按鈕顯示的顏色
如何引用這個(gè)資源呢?
只需在xml文件對(duì)應(yīng)的按鈕中使用?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包中創(chuàng)建一個(gè)btn_image_menu.xml文件,且在包內(nèi)放置兩張圖片
?
? ? ? ? ? w4.jpg w5.jpg
?
btn_image_menu.xml
描述:
android:state_checked="false" 表示沒有點(diǎn)擊這個(gè)圖像時(shí)默認(rèn)顯示的圖片就是它
android:state_checked="true" 表示當(dāng)點(diǎn)擊了這個(gè)圖像時(shí)圖像顯示的樣子
<?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>?
//四個(gè)參數(shù)含義:x、y、width、height轉(zhuǎn)載于:https://www.cnblogs.com/zn615/p/8184654.html
總結(jié)
以上是生活随笔為你收集整理的Android开发 ---如何操作资源目录中的资源文件2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 设计模式笔记-命令模式
- 下一篇: createPattern() 自定义宽