这篇文章将为大家详细讲解有关Android Spinner和GridView组件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一. 概述: &nbs
这篇文章将为大家详细讲解有关Android Spinner和GridView组件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
Spinner是我们所熟悉的下拉框列表。与ListView类似,我们必须为Spinner对象指定一个Adapter。我们从Spinner的简单用法开始讲述。
MainActivity.java
package com.example.demo03_29; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.view.View;import android.widget.Adapter;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { TextView choice; ArrayAdapter<CharSequence> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); choice=(TextView) this.findViewById(R.id.choice); Spinner spinner=(Spinner)this.findViewById(R.id.sp1); adapter=ArrayAdapter.createFromResource(this, R.array.habit, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { choice.setText(adapter.getItem(i)); } @Override public void onNothingSelected(AdapterView<?> adapterView) { }}
在values包下的 string.xml
<resources> <string name="app_name">Demo03-29</string><string-array name="habit"> <item>10公里夜跑</item> <item>爬山</item> <item>游泳</item> <item>吃饭</item> <item>打代码</item></string-array></resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你喜欢的运动"/> <Spinner android:id="@+id/sp1" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="30dp" android:background="#05ADF8"/> <TextView android:id="@+id/choice" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" /></LinearLayout>
sipnner_down.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/p01" android:layout_width="64dp" android:layout_height="64dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/t2" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
这个布局比较简单,就是在LinearLayout中放置一个LinearLayout、一个分隔组件用的View和一个显示所选结果的TextView。在其中嵌套的LinearLayout中放置一个用于提示用的TextView和下拉框Spinner。
MyAdapter.java
package com.example.demo03_29; import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView; import androidx.annotation.NonNull; public class MyArrayApater extends ArrayAdapter { private LayoutInflater minflater; String[] titles; String[] desc={ "集成工具", "高速保存,随时存储", "优质资源,尽在迅雷", "集成开发工具", "学习神器" }; int[] photos={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5}; public MyArrayApater(@NonNull Context context, int resource, CharSequence[] objects) { super(context, resource, objects); minflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); titles=context.getResources().getStringArray(R.array.habit); } @Override public int getCount(){ return titles.length; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent){ View v; v=minflater.inflate(R.layout.spinner_down,parent,false); ImageView iv=(ImageView)v.findViewById(R.id.p01); iv.setImageResource(photos[position]); TextView tv01=(TextView) v.findViewById(R.id.t1); tv01.setText(titles[position]); TextView tv02=(TextView) v.findViewById(R.id.t2); tv02.setText(desc[position]); return v; }}
修改 MainActivity.java
GridView以二维表格的方式显示数据,若数据比较多,该组件将提供垂直滚动条。我们用一个列子来说明GridView的使用方法。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <GridView android:id="@+id/gr1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="4" /> </LinearLayout>
ImgeAdapter.java
package com.example.demo0330; import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private int[] images={ R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, }; private Context context; public ImageAdapter(Context context){ this.context=context; } @Override public int getCount() { return images.length; } @Override public Object getItem(int position) { return images[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView==null){ imageView=new ImageView(context); int width= GridView.LayoutParams.MATCH_PARENT; int heght= GridView.LayoutParams.MATCH_PARENT; imageView.setLayoutParams(new ViewGroup.LayoutParams(width,heght)); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); }else{ imageView=(ImageView) convertView; } imageView.setImageResource(images[position]); return imageView; }}
MainActivity.java
package com.example.demo0330; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.GridView;import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridView=(GridView) findViewById(R.id.gr1); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Toast.makeText(MainActivity.this,"现在点击的是位置在"+(position+1)+"张照片",Toast.LENGTH_LONG).show(); } }); }}
效果图:
关于“Android Spinner和GridView组件怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
--结束END--
本文标题: Android Spinner和GridView组件怎么用
本文链接: https://www.lsjlt.com/news/326073.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0