Как изменить стиль фона / цвет только одного элемента в GalleryView / GridView? - PullRequest
0 голосов
/ 16 ноября 2011

У меня есть виджет GalleryView в моем приложении для Android, и я хотел бы изменить стиль / цвет фона центрального элемента в виджете, чтобы он выделялся на фоне остальных.Еще лучше, если бы я мог сделать этот центральный предмет большего размера, чем остальные.

Есть идеи?

Ответы [ 3 ]

0 голосов
/ 16 ноября 2011

Cerate a selector и установите его в качестве фона элементов галереи. в селекторе установите BackgroundColor и другие функции для выбранного состояния.

selector.xml

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

 </selector>

gallery_item.xml:

<LinearLayout backgrond=selector.xml >

--
--

</LinearLayout >
0 голосов
/ 04 июля 2012

Если вы видите исходный код 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>
0 голосов
/ 16 ноября 2011

Вы можете установить цвет фона (на ваш выбор), например, у вас есть 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-го текста редактирования таким же образом, как вы можете это сделать

...