编辑文本

android:hint=“Please input something”
输入中提示

单选框
应该放到

下拉框
数据放到values strings.xml里

Kate
Jame
Kobe

写好后调用
android:entries="@array/mysp_arry"

动态填充list中数据的方法
把数组对应到listview中
首先把java文件中listview和xml文件中的绑定
lv2=findViewById(R.id.lv2);
适配器 把数组对应到listview中去
ArrayAdapter arrad =new ArrayAdapter (ListActivity.this,android.R.layout.simple_list_item_1,sp_arry);
activity 什么布局 装载哪个数据
适配器赋值给listview
lv2.setAdapter(arrad);

自定义控件

图片视图

在listactivity.java中
public ListView lv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
自定义视图
在code中寻找方法


适配器代码
public View getView(int position, View v, ViewGroup parent) {
if(position%2==0){
v= LayoutInflater.from(context).inflate(R.layout.listitem1,null);

        }else{            v= LayoutInflater.from(context).inflate(R.layout.listitem2,null);        }        TextView tvv1=v.findViewById(R.id.tvv1);        TextView tvv2=v.findViewById(R.id.tvv2);        tvv1.setText(sp_arry[position]);        tvv2.setText("18");        return v;    }

代码
activity.list

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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=".ListActivity">    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <EditText        android:id="@+id/et"        android:layout_width="match_parent"        android:layout_height="50dp"        android:hint="Please input something"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintTop_toBottomOf="@id/tv" />    <RadioGroup        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:id="@+id/rg"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintTop_toBottomOf="@id/et">        <RadioButton            android:id="@+id/rb1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Female"            app:layout_constraintLeft_toLeftOf="parent"            app:layout_constraintTop_toBottomOf="@id/et" />        <RadioButton            android:id="@+id/rb2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Male"            app:layout_constraintLeft_toRightOf="@id/rb1"            app:layout_constraintTop_toBottomOf="@id/et" />    </RadioGroup>    <CheckBox        android:id="@+id/cb1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Red"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintTop_toBottomOf="@id/rg" />    <CheckBox        android:id="@+id/cb2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Red"        app:layout_constraintLeft_toRightOf="@+id/cb1"        app:layout_constraintTop_toBottomOf="@id/rg" />    <Spinner        android:layout_width="match_parent"        android:layout_height="50dp"        app:layout_constraintTop_toBottomOf="@id/cb1"        app:layout_constraintLeft_toLeftOf="parent"        android:entries="@array/mysp_arry"        android:id="@+id/sp"/>    <ListView        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:layout_constraintTop_toBottomOf="@id/sp"        app:layout_constraintLeft_toLeftOf="parent"        android:entries="@array/mysp_arry"        android:id="@+id/lv1"/>    <ListView        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:layout_constraintTop_toBottomOf="@id/lv1"        app:layout_constraintLeft_toLeftOf="parent"        android:id="@+id/lv2"/>    <ListView        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:layout_constraintTop_toBottomOf="@id/lv2"        app:layout_constraintLeft_toLeftOf="parent"        android:id="@+id/lv3"/></androidx.constraintlayout.widget.ConstraintLayout>

List.activity.java

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.os.Bundle;import android.text.Layout;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class ListActivity extends AppCompatActivity {    public String[] sp_arry={"Kobe","Jame","Kate"};    public ListView lv2;    public ListView lv3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_list);        lv2=findViewById(R.id.lv2);        ArrayAdapter<String> arrad =new ArrayAdapter<String> (ListActivity.this,android.R.layout.simple_list_item_1,sp_arry);        lv2.setAdapter(arrad);        lv3=findViewById(R.id.lv3);        lv3.setAdapter(new Myadapter(ListActivity.this));    }    private class Myadapter extends BaseAdapter{        Context context;        public Myadapter(Context context) {            super();            this.context = context;        }        public Myadapter() {                super();        }        @Override        public int getCount() {            return sp_arry.length;        }        @Override        public Object getItem(int position) {            return position;        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View v, ViewGroup parent) {            if(position%2==0){                v= LayoutInflater.from(context).inflate(R.layout.listitem1,null);            }else{                v= LayoutInflater.from(context).inflate(R.layout.listitem2,null);            }            TextView tvv1=v.findViewById(R.id.tvv1);            TextView tvv2=v.findViewById(R.id.tvv2);            tvv1.setText(sp_arry[position]);            tvv2.setText("18");            return v;        }    }}

listitem1.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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:background="#00ff00"    tools:context=".ListItem1">    <ImageView        android:layout_width="80dp"        android:layout_height="80dp"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        android:id="@+id/iv"        android:src="@mipmap/ic_launcher"/>        <TextView        android:layout_width="wrap_content"        android:layout_height="40dp"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintLeft_toRightOf="@+id/iv"        android:id="@+id/tvv1"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="40dp"        app:layout_constraintTop_toBottomOf="@id/tvv1"        app:layout_constraintLeft_toRightOf="@+id/iv"        android:id="@+id/tvv2"/></androidx.constraintlayout.widget.ConstraintLayout>

listitem2.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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:background="#0000ff"    tools:context=".ListItem1">    <ImageView        android:layout_width="80dp"        android:layout_height="80dp"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        android:id="@+id/iv"        android:src="@mipmap/ic_launcher"/>        <TextView        android:layout_width="wrap_content"        android:layout_height="40dp"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintLeft_toRightOf="@+id/iv"        android:id="@+id/tvv1"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="40dp"        app:layout_constraintTop_toBottomOf="@id/tvv1"        app:layout_constraintLeft_toRightOf="@+id/iv"        android:id="@+id/tvv2"/></androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

<resources>    <string name="app_name">My Application</string>    <string-array name="mysp_arry">        <item>Kate</item>        <item>Jame</item>        <item>Kobe</item>    </string-array></resources>

更多相关文章

  1. android ViewFlipper
  2. Android的NDK开发(3)————JNI数据类型的详解
  3. Android计算器
  4. ANDROID视图空间代码
  5. android的ListView显示
  6. Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值
  7. [Android]Android(安卓)Design之Navigation Drawer
  8. Android开发实例详解之IMF(Android(安卓)SDK Sample—SoftKeyboar
  9. android平板上的GridView视图缓存优化

随机推荐

  1. android 小部件 AndroidManifest.xml
  2. Android自学笔记(番外篇):全面搭建Linux环境
  3. Android(安卓)Intent
  4. android 2.3 电量管理
  5. QT Creator 构建android apk失败问题
  6. Linux 下进行Android开发环境搭建
  7. android TabWidget 位置
  8. Android练习—修改背景颜色
  9. Android 中支持的几种传感器
  10. 【android】shape的使用