获取Drawable对象:
Resources res = mContext.getResources();
Drawable myImage = res.getDrawable(R.drawable.my_image);
selector是在文件夹drawable中进行定义的xml文件。
它主要定义控件在下pressed,selected,focused及平常状态下的属性。
越前面定义的状态,其优先级越高。对定得某个状态,如果某个属性没有显示说明,则表示此时该属性为任意值,都没关系。
示例1:
定义背景图片
bg_selector.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/listlayoutsample12xml_press_image" />
<item
android:state_selected="true"
android:drawable="@drawable/listlayoutsample12xml_focus_image" />
<item
android:state_focused="true"
android:drawable="@drawable/listlayoutsample12xml_focus_image" />
<item
android:drawable="@drawable/listlayoutsample12xml_bg_image" />
</selector>
在布局文件中使用它
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_selector"
android:paddingRight="13.33dp"
android:layout_height="65.33dp"
android:paddingLeft="31.9dp"
android:gravity="center_vertical">
</LinearLayout>
示例2:
定义背景图片
bg_selector.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/listlayoutsample12xml_press_image" />
<item
android:state_selected="true"
android:drawable="@drawable/listlayoutsample12xml_focus_image" />
<item
android:state_focused="true"
android:drawable="@drawable/listlayoutsample12xml_focus_image" />
<item
android:drawable="@drawable/listlayoutsample12xml_bg_image" />
</selector>
在布局文件中使用它
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_selector"
android:paddingRight="13.33dp"
android:layout_height="65.33dp"
android:paddingLeft="31.9dp"
android:gravity="center_vertical">
</LinearLayout>
示例2:
定义selector的颜色
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@touchwiz:color/tw_color001" />
<item android:state_selected="true" android:color="@touchwiz:color/tw_color001" />
<item android:state_focused="true" android:color="@touchwiz:color/tw_color001"/>
<item android:color="@touchwiz:color/tw_color003"/>
</selector>
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@touchwiz:color/tw_color001" />
<item android:state_selected="true" android:color="@touchwiz:color/tw_color001" />
<item android:state_focused="true" android:color="@touchwiz:color/tw_color001"/>
<item android:color="@touchwiz:color/tw_color003"/>
</selector>
//---------------------------------------
正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示:
实现起来非常简单。
第一步,创建一个背景配置文件float_box.xml,放到res/drawable下,如下所示(如看不懂可查看本站:):
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- ** Copyright 2010, Ideasandroid
- -->
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#ffffff" />
- <stroke android:width="3dp" color="#000000" />
- <corners android:radius="3dp" />
- <padding android:left="10dp" android:top="10dp" android:right="10dp"
- android:bottom="10dp" />
- </shape>
第二步,定义一个对话框样式,放到res/values/styles.xml,如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- Copyright (C) 2010 IdeasAndroid
- -->
- <resources>
- <!-- 定义一个样式,继承android系统的对话框样式 android:style/Theme.Dialog-->
- <style name="Theme.FloatActivity" parent="android:style/Theme.Dialog">
- <!-- float_box为我们定义的窗口背景-->
- <item name="android:windowBackground">@drawable/float_box</item>
- </style>
- </resources>
第三步,创建一个视图配置文件res/layout/float_activity.xml,一个ImageView和一个TextView,如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- Copyright (C) 2010 IdeasAndroid
- -->
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/ideasandroidlogo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:src="@drawable/ideasandroid"
- />
- <TextView android:layout_width="wrap_content" android:text="@string/ideasandroidIntr"
- android:layout_height="wrap_content"
- android:layout_below="@id/ideasandroidlogo"
- android:textColor="@android:color/black"
- />
- </RelativeLayout>
第四步创建我们的Activity,如下所示:
- public class FloatActivityDemo extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //先去除应用程序标题栏 注意:一定要在setContentView之前
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- //将我们定义的窗口设置为默认视图
- setContentView(R.layout.float_activity);
- }
- }
最后一步,更改应用程序配置文件AndroidManifest.xml,将我们刚才创建的样式应用到我们的Activity上,如下所示:
- <activity android:name=".FloatActivityDemo" android:theme="@style/Theme.FloatActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>