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

发布于 12-16 00:27 字数 119 浏览 4 评论 0原文

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

有什么想法吗?

I have a GalleryView widget in my android app and I would like to change the background style/color of the central item in the widget, so that it stands out from the rest. Even better, if I could make this central item bigger size than the rest.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狠疯拽2024-12-23 00:27:58

创建一个选择器并将其设置为图库项目的背景。在选择器中,设置选定状态的背景颜色和其他功能。

选择器.xml

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

 </selector>

gallery_item.xml:

<LinearLayout backgrond=selector.xml >

--
--

</LinearLayout >

Cerate a selector and set it as background of gallery items . in selector, set BackgroundColor and other features for selected state .

selector.xml

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

 </selector>

gallery_item.xml :

<LinearLayout backgrond=selector.xml >

--
--

</LinearLayout >
×纯※雪2024-12-23 00:27:58

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

If you see the Android SDK source code, You should find the following directories and files:

/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

You can copy these files to your project and create an adapter like this:

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;
    }

}

Then edit xml and png files in resources directory like this:

<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>
疧_╮線2024-12-23 00:27:58

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

正如您所看到的,我已经以同样的方式更改了第三个编辑文本的背景颜色,您也可以这样做

You can set the backgorund colour (ur choice) in the specific layout
for example you have 5 edit box 2 above 2 below and 1 in center
whom u wanna change the background color u can do the following

<?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>

As you can see i hav changed the background colour of 3rd edit text in the same way u can also do that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文