Android新浪微博開発(五)androidにおけるselector,背景セレクタ
1まずSDKでどう言っているか見てみましょう
StateListDrawable
オブジェクトは,オブジェクトの状態に応じて異なるイメージで同じgrapics,例えばButtonを表現する.1つのres/drawable/filename.xml , :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=[ " true " false " ]
android:dither=[ " true " false "]
android:variablePadding=[ " true " false "]>
<item android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=[ " true " false "]
android:state_focused=[ "true " false "]
android:state_selected=[ " true " false "]
android:state_checkable=[ " true " false "]
android:state_checked=[ "true " false "]
android:state_enabled=[ " true " false "]
android:state_window_focused=[ " true " false "] />
</selector>
属性の説明:
<selector>
はルート要素でなければなりません.<item>
は、その属性を記述することによって、特定の状態におけるdrawableを定義する.<selector>
のサブエレメントでなければなりません. android:state_pressed: , ;
android:state_focused: ;
android:state_selected: , tab ;
android:state_checkable: checkable ; android:state_checked;
android:state_enabled: , ;
android:state_window_focused: 。
Android , 。
2 Tabボタンとlistitemの を<selector>
tabのselector は のとおりです.<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/home_normal" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/home_active" />
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/home_normal" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/home_active" />
<item android:state_pressed="true" android:drawable="@drawable/home_normal" />
<item android:drawable="@drawable/home_normal" />
</selector>
に の1つを えて、 に していないので、 にエラーが されないようにします.
にtabを する に え、setIndicatorには3つのリロードがあるため、 できるパラメータ は:(CharSequence label)1つの ;(CharSequence label,Drawable icon) にアイコンを ける.(View view)、1つの で、 にすることができて、それからやはりInflateでxmlファイルから て、コードは の りです: // Activity
View view1 = (View)LayoutInflater.from(MainActivity.this).inflate(R.layout.tabitem, null);
((ImageView)view1.findViewById(R.id.tabimage)).setImageResource(R.drawable.hometab);
((TextView)view1.findViewById(R.id.tabtitle)).setText(WEIBO_HOME);
it = new Intent(MainActivity.this,HomeTimeLineActivity.class);
tspec = tabhost.newTabSpec(WEIBO_HOME).setIndicator(view1).setContent(it);
tabhost.addTab(tspec);
tabitem.xmlは ですImageViewとTextViewです<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/tabimage"
android:layout_width="fill_parent" android:layout_height="32.0dip"
android:scaleType="fitCenter" />
<TextView android:id="@+id/tabtitle"
android:textSize="11.0sp" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="wrap_content"
/>
</LinearLayout>
な は のとおりです.
はやはり で、 はしにくいで、ピクチャーは しにくいです