Android Studio代码笔记09.自定义视图

编辑文本

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中寻找方法
Android Studio代码笔记09.自定义视图_第1张图片
Android Studio代码笔记09.自定义视图_第2张图片
适配器代码
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>

Android Studio代码笔记09.自定义视图_第3张图片

你可能感兴趣的:(Android)