drawable顏色常數的定義

 代碼:

package com.oyzz.ch3_2;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.EditText;

public class Ch3_2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //得到color.xml文件中的顏色
        Resources  res = getBaseContext().getResources();
        Drawable d = res.getDrawable(R.drawable.other);
       
        //得到控件對象
        EditText et = (EditText) findViewById(R.id.txtaccount);
        et.setBackgroundDrawable(d);
       
    }
}

 

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main"
    android:background="@drawable/white"
    >
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lblaccount"
    android:id="@+id/lblaccount"
    android:layout_x="61px"
    android:layout_y="69px"
    android:textColor="@drawable/lbl"
 />
 <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lblpass"
    android:id="@+id/lblaccount1"
    android:layout_x="61px"
    android:layout_y="158px"
    android:textColor="@drawable/lbl"
 />
 <EditText
    android:layout_width="120dip"
    android:layout_height="wrap_content"
    android:id="@+id/txtaccount"
    android:layout_x="114px"
    android:textSize="10sp"
    android:layout_y="57px"
 />
 <EditText
    android:layout_width="120dip"
    android:layout_height="wrap_content"
    android:id="@+id/txtaccount1"
    android:layout_x="112px"
    android:textSize="10sp"
    android:layout_y="142px"
    android:password="true"
 />
</AbsoluteLayout>

color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <drawable name="white">#FFFFFFFF</drawable>
 <drawable name="lbl">#F000FFFF</drawable>
 <drawable name="other">#F111FFFF</drawable>
</resources>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="lblaccount">賬號</string>
    <string name="lblpass">密碼</string>
    <string name="app_name">drawable定義顏色</string>
</resources>

說明:

android sdk默認的顏色是深黑色

1.定義顏色是到values/color.xml文件中,定義的格式爲:
  <drawable name="">value</drawable>
 
到main.xml配置文件中應用是用到@drawable/drawable_name(color.xml文件中的名稱) 

2.定義字符串也是一樣的到values/string.xml文件中,定義的格式爲:
<string name="">value</string>

到main.xml配置文件中應用是用到@string/string_name(strings.xml文件中的名稱)
 
2.通過代碼得到配置中的顏色:

   Resources  res = getBaseContext().getResources();
        Drawable d = res.getDrawable(R.drawable.other<color.xml文件中的名稱>);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章