AndroidアプリにGalleryViewウィジェットがあり、ウィジェットの中央アイテムの背景スタイル/色を変更して、残りの部分から目立つようにしたいと思います。さらに良いことに、この中心的なアイテムを他のアイテムよりも大きなサイズにすることができれば.
何か案は?
AndroidアプリにGalleryViewウィジェットがあり、ウィジェットの中央アイテムの背景スタイル/色を変更して、残りの部分から目立つようにしたいと思います。さらに良いことに、この中心的なアイテムを他のアイテムよりも大きなサイズにすることができれば.
何か案は?
aselector
を作成し、ギャラリー アイテムの背景として設定します。セレクターで、選択された状態の BackgroundColor およびその他の機能を設定します。
セレクター.xml
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
</selector>
gallery_item.xml :
<LinearLayout backgrond=selector.xml >
--
--
</LinearLayout >
Android SDK ソース コードが表示されている場合は、次のディレクトリとファイルが見つかるはずです。
/opt/android-sdk-linux/platforms/android-10/data/res/drawable-hdpi/gallery_*
/opt/android-sdk-linux/platforms/android-10/data/res/drawable/gallery_item_background.xml
これらのファイルをプロジェクトにコピーして、次のようなアダプターを作成できます。
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class ImageAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private final ImageDownloader imageDownloader = new ImageDownloader();
public ImageAdapter(Context context, int layoutItem,List<ImageItem> objects) {
super(context, layoutItem, objects);
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(R.drawable.gallery_item_background);
} else {
imageView = (ImageView) convertView;
}
imageDownloader.download(getItem(position).thumbnail, imageView); // or similar function to set imageView.
return imageView;
}
}
次に、リソース ディレクトリ内の xml および png ファイルを次のように編集します。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When the window does not have focus. -->
<item android:drawable="@drawable/gallery_selected_default"
android:state_selected="true"
android:state_window_focused="false"/>
<item android:drawable="@drawable/gallery_unselected_default"
android:state_selected="false"
android:state_window_focused="false"/>
<!-- When the window does have focus. -->
<item android:drawable="@drawable/gallery_selected_pressed"
android:state_selected="true"
android:state_pressed="true"/>
<item android:drawable="@drawable/gallery_selected_focused"
android:state_selected="true"
android:state_focused="true"/>
<item android:drawable="@drawable/gallery_selected_default"
android:state_selected="true"/>
<item android:drawable="@drawable/gallery_unselected_pressed"
android:state_selected="false"
android:state_pressed="true"/>
<item android:drawable="@drawable/gallery_unselected_default"/>
</selector>
特定のレイアウトでバックゴーンドの色(あなたの選択)を設定できます。たとえば、5つの編集ボックスが2つ上に2つ下にあり、中央に1つあり、背景色を変更したい人は次のことができます
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="#000000"
android:orientation="vertical"
android:padding="5px" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/id1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/id2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#435232"
android:orientation="horizontal" >
<EditText
android:id="@+id/id3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:digits="0123456789"
android:maxLength="6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/id4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/id5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:digits="0123456789"
android:maxLength="11" />
</LinearLayout>
</LinearLayout>
ご覧のとおり、3番目の編集テキストの背景色を同じ方法で変更しました。それも可能です