<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><ImageViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="3月29日"android:paddingTop="10dp"android:paddingBottom="10dp"/><GridViewandroid:id="@+id/gridView"android:layout_width="match_parent"android:layout_height="match_parent"android:numColumns="auto_fit"android:gravity="center"android:verticalSpacing="5dp"android:columnWidth="100dp"/></LinearLayout>
package com.example.qqablum;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.media.Image;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {private int [] picture=new int[]{R.drawable.aa,R.drawable.bb,,R.drawable.dd,R.drawable.ee,R.drawable.ff};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);GridView gridView=findViewById(R.id.gridView);gridView.setAdapter(new ImageAdpater(this));}public class ImageAdpater extends BaseAdapter{private Context mContext;public ImageAdpater(Context c){mContext=c;}@Overridepublic int getCount() {return picture.length;}@Overridepublic Object getItem(int i) {return null;}@Overridepublic long getItemId(int i) {return 0;}@Overridepublic View getView(int position, View converView, ViewGroup parent) {ImageView imageView;if(converView==null){imageView=new ImageView(mContext);imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);}else{imageView=(ImageView) converView;}imageView.setImageResource(picture[position]);return imageView;}}}
老感觉我写错了,效果图不太一样
下拉列表框:方法一:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Spinnerandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="@array/ctype"/></androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?><resources><string-array name="ctype"><item>全部</item><item>电影</item><item>图书</item><item>游戏</item></string-array></resources>
在value创建一个arrays.xml的文件
下拉列表:方法二:
package ;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String []ctype=new String[]{"全部","美术","音乐","体育"};ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ctype);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);Spinner spinner=findViewById(R.id.spinner);spinner.setAdapter(adapter);String str=spinner.getSelectedItem().toString();Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();}}
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Spinnerandroid:id="@+id/spinner"android:layout_width="wrap_content"android:layout_height="wrap_content"/></androidx.constraintlayout.widget.ConstraintLayout>
package ;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Spinner spinner=findViewById(R.id.spinner);spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {String result=parent.getItemAtPosition(position).toString();Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}});}}
添加一个监听器,点击会有显示效果
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".MainActivity"><Spinnerandroid:id="@+id/spinner"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="@array/ctype"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="搜素"android:textColor="#1010D5"/></LinearLayout>
列表视图:方法一:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ListViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:entries="@array/ctype"/></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><resources><string-array name="ctype"><item>全部</item><item>电影</item><item>图书</item><item>游戏</item></string-array></resources>
方法二:
package com.example.nnn;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.Adapter;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String[]ctype=new String[]{"全部","游戏","图书","电视"};ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ctype);ListView listView=findViewById(R.id.listView);listView.setAdapter(adapter);}}
实现效果一样
学习安卓开发第七天【网格视图qq相册页面】【下拉;列表框】【下拉列表框实例】【列表视图】