如何更改 GalleryView/GridView 中一项的背景样式/颜色?
我的 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 技术交流群。

发布评论
评论(3)
×纯※雪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>
疧_╮線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>
正如您所看到的,我已经以同样的方式更改了第三个编辑文本的背景颜色,您也可以这样做
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
创建一个
选择器
并将其设置为图库项目的背景。在选择器中,设置选定状态的背景颜色和其他功能。选择器.xml
gallery_item.xml:
Cerate a
selector
and set it as background of gallery items . in selector, set BackgroundColor and other features for selected state .selector.xml
gallery_item.xml :