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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM