如何在GalleryView / GridView中仅更改一个项目的背景样式/颜色?

时间:2011-11-16 05:11:12

标签: android xml galleryview

我的Android应用程序中有一个GalleryView小部件,我想更改小部件中的中心项目的背景样式/颜色,以便从其余部分中脱颖而出。更好的是,如果我能让这个中心项目比其他项目更大。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您可以在特定布局中设置backgorund颜色(您的选择) 例如,你有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>

正如你所看到的,我改变了第三编辑文本的背景颜色,你也可以这样做

答案 1 :(得分:0)

Cerate a selector并将其设置为图库项目的背景。在选择器中,为选定状态设置BackgroundColor和其他功能。

selector.xml

     

     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->

 </selector>

gallery_item.xml:

<LinearLayout backgrond=selector.xml >

--
--

</LinearLayout >

答案 2 :(得分:0)

如果您看到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>