Android基础——高级UI组件:下拉框,列表,滚动条视图

布局文件

<?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"
    android:orientation="vertical"
    >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/ctype"
        />

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="370dp"
        android:entries="@array/ctype" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/content"
                android:textSize="50sp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/content"
                android:textSize="50sp" />
        </LinearLayout>

    </HorizontalScrollView>

</LinearLayout>

 

java实现 视图-适配器-资源 

package com.example.myhighuiiiii;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

//通过适配器来实现
public class MainActivity extends AppCompatActivity {

    Spinner spinner = null;
    ListView listView = null;

    @Override
    protected 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 string = spinner.getSelectedItem().toString();
        Toast.makeText(this,string,Toast.LENGTH_SHORT);

        /*
        * 列表视图实现
        * */
        String[] cctype = new String[]{
                "全部","图书","游戏","电视"
        };
        //创建一个适配器
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
                this,android.R.layout.simple_list_item_1,cctype
        );
        //将适配器和listView相关联
       listView = (ListView) findViewById(R.id.listView);
       listView.setAdapter(adapter);
    }
}

 

上一篇:Android studio listview实现列表数据显示 数据循环显示


下一篇:Android实战项目——家庭记账本(二)