0


Android(基本、高级UI组件)

一:前言

我们在布局管理器中已经知道了ImageView、TextView、Button等组件,在这里我们将会详细的介绍Android中基本组件。

二:文本框组件

文本框组件的作用是在页面中添加一些文本信息

文本框的基本代码

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="@string/login"
  5. android:textSize="19sp"
  6. android:textColor="#FF0000"
  7. android:singleLine="true"
  8. android:maxWidth="180dp"
  9. />

三:编辑框组件

编辑框组件的作用是提供给用户输入一些信息

  1. <EditText
  2. android:id="@+id/it"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:hint="密码"
  6. android:inputType="textPassword"
  7. android:drawableLeft=""
  8. android:drawableRight=""
  9. android:drawableBottom=""
  10. android:drawabletop=""
  11. android:drawablePadding=""
  12. <!-- 向下拖动-->
  13. android:lines=""
  14. />

我们可以在java类中通过

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. EditText editText = findViewById(R.id.it);
  7. Editable text = editText.getText();
  8. }
  9. }

四:按钮组件

按钮的更多作用时触发事件监听器

4.1 匿名内部类监听器

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Button button = findViewById(R.id.b1);
  7. button.setOnClickListener(new View.OnClickListener() {
  8. @Override
  9. public void onClick(View v) {
  10. Toast.makeText(MainActivity.this,"单击了",Toast.LENGTH_SHORT).show();
  11. }
  12. });
  13. }
  14. }

4.2 onClick属性实现

  1. public void onClick(View view){
  2. Toast.makeText(MainActivity.this,"单击了",Toast.LENGTH_SHORT).show();
  3. }

使用这种方法需要在布局文件中指定声明才能使用

  1. <Button
  2. android:id="@+id/b2"
  3. android:onClick="myClick"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:text="按钮2"
  7. />

4.3 图像按钮(Imagebutton)

  1. <ImageButton
  2. android:id="@+id/b2"
  3. android:src="@drawable/p1"
  4. android:onClick="myClick"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:background="#0000"
  8. android:text="按钮2"
  9. />

4.4 单选按钮(radioButton)

单选按钮的xml文件设置

  1. <RadioGroup
  2. android:id="@+id/button1"
  3. android:layout_height="166dp"
  4. android:layout_width="79dp">
  5. <RadioButton
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:text="男"
  9. />
  10. <RadioButton
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="女"
  14. />
  15. <RadioButton
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="其他"
  19. />
  20. </RadioGroup>
  21. <Button
  22. android:id="@+id/button2"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="提交"
  26. />

java代码

  1. package com.example;
  2. import android.view.View;
  3. import android.widget.Button;
  4. import android.widget.RadioButton;
  5. import android.widget.RadioGroup;
  6. import android.widget.Toast;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. RadioGroup radioGroup = (RadioGroup) findViewById(R.id.button1);
  15. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  16. @Override
  17. public void onCheckedChanged(RadioGroup group, int checkedId) {
  18. RadioButton radioButton = findViewById(checkedId);
  19. CharSequence text = radioButton.getText();
  20. Toast.makeText(MainActivity.this, "性别" + text, Toast.LENGTH_SHORT).show();
  21. }
  22. });
  23. Button button = findViewById(R.id.button2);
  24. button.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. for (int i = 0; i < radioGroup.getChildCount(); i++) {
  28. RadioButton r = (RadioButton) radioGroup.getChildAt(i);
  29. if(r.isChecked()){
  30. Toast.makeText(MainActivity.this, "性别" + r.getText(), Toast.LENGTH_SHORT).show();
  31. }
  32. }
  33. }
  34. });
  35. }
  36. }

4.5 复选框按钮(CheckBox)

复选框按钮的xml文件

  1. <CheckBox
  2. android:id="@+id/cb1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="跳水"
  6. />
  7. <CheckBox
  8. android:id="@+id/cb2"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="游泳"
  12. />
  13. <CheckBox
  14. android:id="@+id/cb3"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="羽毛球"
  18. />

java程序代码

  1. package com.example;
  2. import android.view.View;
  3. import android.widget.*;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. public class MainActivity extends AppCompatActivity {
  7. CheckBox checkBox;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. checkBox = (CheckBox) checkBox.findViewById(R.id.cb1);
  13. checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  14. @Override
  15. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  16. }
  17. });
  18. }
  19. }

五:日期选择器(DatePicker)

xml文件代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="horizontal"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <DatePicker
  11. android:id="@+id/date"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. />
  15. </LinearLayout>

java程序代码

  1. package com.example;
  2. import android.app.Activity;
  3. import android.widget.DatePicker;
  4. import android.widget.Toast;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import java.util.Calendar;
  8. public class MainActivity extends Activity {
  9. int year, mouth, data;
  10. DatePicker datePicker;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. datePicker = (DatePicker) findViewById(R.id.date);
  16. Calendar calendar = Calendar.getInstance();
  17. year = calendar.get(Calendar.YEAR);
  18. mouth = calendar.get(Calendar.MONTH);
  19. data = calendar.get(Calendar.DAY_OF_MONTH);
  20. datePicker.init(year, mouth, data, new DatePicker.OnDateChangedListener() {
  21. @Override
  22. public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  23. MainActivity.this.year = year;
  24. MainActivity.this.mouth = monthOfYear;
  25. MainActivity.this.data = dayOfMonth;
  26. show(year,mouth,data);
  27. }
  28. });
  29. }
  30. public void show(int year,int mouth,int date){
  31. String s = "年:" + year + "月:" + (mouth+1) + "日:" + date;
  32. Toast.makeText(MainActivity.this,s, Toast.LENGTH_LONG).show();
  33. }
  34. }

六:时间选择器(timePicker)

xml文件代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="horizontal"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <TimePicker
  11. android:id="@+id/date"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. />
  15. </LinearLayout>

java程序代码

  1. package com.example;
  2. import android.app.Activity;
  3. import android.widget.DatePicker;
  4. import android.widget.TimePicker;
  5. import android.widget.Toast;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import java.util.Calendar;
  9. public class MainActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. TimePicker timePicker = findViewById(R.id.date);
  15. //24小时设置
  16. timePicker.setIs24HourView(true);
  17. timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
  18. @Override
  19. public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
  20. String s = "时:" + hourOfDay + "分" + minute;
  21. Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
  22. }
  23. });
  24. }
  25. }

七:计时器 (Chronometer)

xml文件代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="horizontal"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <Chronometer
  11. android:id="@+id/date"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:textColor="#E91E63"
  15. android:layout_marginTop="10dp"
  16. android:layout_marginLeft="10dp"
  17. android:layout_marginRight="10dp"
  18. android:layout_alignParentRight="true"
  19. android:format="已用时间:%s"
  20. />
  21. </RelativeLayout>

java程序代码

  1. package com.example;
  2. import android.app.Activity;
  3. import android.os.SystemClock;
  4. import android.view.WindowManager;
  5. import android.widget.Chronometer;
  6. import android.os.Bundle;
  7. import java.util.Calendar;
  8. public class MainActivity extends Activity {
  9. Chronometer chronometer;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  15. chronometer=findViewById(R.id.date);
  16. //设置系统时间
  17. chronometer.setBase(SystemClock.elapsedRealtime());
  18. //设置计时器的格式
  19. chronometer.setFormat("%s");
  20. //开启计时器
  21. chronometer.start();
  22. //设置监听器
  23. chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
  24. @Override
  25. public void onChronometerTick(Chronometer chronometer) {
  26. if(SystemClock.elapsedRealtime() - chronometer.getBase() >= 60000){
  27. chronometer.stop();
  28. }
  29. }
  30. });
  31. }
  32. }

八:进度条组件(progressBar)

水平进度条(实时加载)

圆形进度条(正在加载)

进度条与线程搭配才能实现实施更新,在anroid中handler的作用是给主线程发送消息,因为主线程不支持android的activity在主线程中更新UI。

xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="horizontal"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <ProgressBar
  11. style="?android:attr/progressBarStyleHorizontal"
  12. android:max="100"
  13. android:progress="50"
  14. android:id="@+id/date"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentBottom="true"
  18. android:layout_marginBottom="30sp"
  19. />
  20. </RelativeLayout>

java程序代码

  1. package com.example;
  2. import android.app.Activity;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.os.SystemClock;
  6. import android.view.View;
  7. import android.view.WindowManager;
  8. import android.widget.Chronometer;
  9. import android.os.Bundle;
  10. import android.widget.ProgressBar;
  11. import android.widget.Toast;
  12. import androidx.annotation.NonNull;
  13. import java.util.Calendar;
  14. import java.util.Random;
  15. public class MainActivity extends Activity {
  16. private ProgressBar progressBar;
  17. private int mprogress = 0;
  18. private Handler mhandler;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  24. ProgressBar progressBar = findViewById(R.id.date);
  25. mhandler = new Handler(){
  26. @Override
  27. public void handleMessage(@NonNull Message msg) {
  28. if(msg.what == 0x111){
  29. progressBar.setProgress(mprogress);
  30. }else{
  31. Toast.makeText(MainActivity.this, "耗时操作完成", Toast.LENGTH_SHORT).show();
  32. progressBar.setVisibility(View.GONE);
  33. }
  34. }
  35. };
  36. new Thread(new Runnable() {
  37. @Override
  38. public void run() {
  39. while (true){
  40. mprogress=dowork();
  41. //更新进度条的进度
  42. Message m = new Message();
  43. if(mprogress < 100){
  44. m.what=0x111;
  45. mhandler.sendMessage(m);
  46. }else{
  47. m.what=0x110;
  48. mhandler.sendMessage(m);
  49. break;
  50. }
  51. }
  52. }
  53. private int dowork() {
  54. mprogress+= Math.random()*10;
  55. try {
  56. Thread.sleep(200);
  57. } catch (InterruptedException e) {
  58. e.printStackTrace();
  59. }
  60. return mprogress;
  61. }
  62. }).start();
  63. }
  64. }

九:拖动条组件(seekBar)

实现一个图片的透明度

xml文件代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="vertical"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <ImageView
  11. android:id="@+id/img"
  12. android:layout_width="match_parent"
  13. android:layout_height="500dp"
  14. android:src="@mipmap/ic_launcher"
  15. />
  16. <SeekBar
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/date"
  20. android:max="10"
  21. android:layout_alignParentBottom="true"
  22. android:thumb="@mipmap/ic_launcher"
  23. android:progress="5"
  24. />
  25. </LinearLayout>

java程序代码

  1. package com.example;
  2. import android.app.Activity;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.os.SystemClock;
  6. import android.view.View;
  7. import android.view.WindowManager;
  8. import android.widget.*;
  9. import android.os.Bundle;
  10. import androidx.annotation.NonNull;
  11. import java.util.Calendar;
  12. import java.util.Random;
  13. public class MainActivity extends Activity {
  14. private ImageView imageView;
  15. private SeekBar seekBar;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  21. imageView = findViewById(R.id.img);
  22. seekBar = findViewById(R.id.date);
  23. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  24. @Override
  25. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  26. imageView.setImageAlpha(progress);
  27. }
  28. @Override
  29. public void onStartTrackingTouch(SeekBar seekBar) {
  30. }
  31. @Override
  32. public void onStopTrackingTouch(SeekBar seekBar) {
  33. }
  34. });
  35. }
  36. }

十:星级评分条(RatingBar)

xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="vertical"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <RatingBar
  11. android:id="@+id/ratebar"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:numStars="5"
  15. android:rating="2"
  16. android:stepSize="0.5"
  17. android:isIndicator="false"
  18. />
  19. </LinearLayout>

java程序代码

  1. package com.example;
  2. import android.app.Activity;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.os.SystemClock;
  6. import android.view.View;
  7. import android.view.WindowManager;
  8. import android.widget.*;
  9. import android.os.Bundle;
  10. import androidx.annotation.NonNull;
  11. import java.util.Calendar;
  12. import java.util.Random;
  13. public class MainActivity extends Activity {
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  19. RatingBar ratingBar = findViewById(R.id.ratebar);
  20. //刚开始几颗星星
  21. String s = String.valueOf(ratingBar.getRating());
  22. Toast.makeText(this, "Rating:" + s, Toast.LENGTH_SHORT).show();
  23. String s1 = String.valueOf(ratingBar.getStepSize());
  24. Toast.makeText(this, "StepSize" + s1, Toast.LENGTH_SHORT).show();
  25. String s2 = String.valueOf(ratingBar.getProgress());
  26. Toast.makeText(this, "getProgress" + s2, Toast.LENGTH_SHORT).show();
  27. }
  28. }

十一:图像视图(ImageView)

xml文件

  1. <ImageView
  2. android:layout_width="500dp"
  3. android:layout_height="200dp"
  4. android:src="@drawable/ic_launcher_background"
  5. android:background="#FFFFFF"
  6. android:maxWidth="300dp"
  7. android:maxHeight="300dp"
  8. android:adjustViewBounds="true"
  9. <!-- 布局-->
  10. android:scaleType="fitXY"
  11. <!-- 着色-->
  12. android:tint="#FFFF00"
  13. android:layout_margin="30dp"
  14. />

java程序代码类似于前面

十二:网格视图适配器(GridView)

12.1 适配器

连接后端数据和前端数据的接口

12.2 适配器的种类

ArrayAdapter:数组适配器将数组的多个值包装成列表项,显示一行文字

SmipleAdapter:简单适配器将list的值包装成列表项

SmipleCusorAdapter:将数据库的内容以列表的形式展示出来

BaseAdapter:对列表项进行定制

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="vertical"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <GridView
  11. android:id="@+id/grid"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. />
  15. </LinearLayout>

十三:下拉列表框(Spinner)

13.1 xml文件设置下拉列表

  1. <Spinner
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:entries="@array/str"
  5. />

values资源文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="str">
  4. <item>全部</item>
  5. <item>电影</item>
  6. <item>图书</item>
  7. <item>游戏</item>
  8. </string-array>
  9. </resources>

13.2 适配器模式设置下拉列表

  1. package com.example;
  2. import android.app.Activity;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.os.SystemClock;
  6. import android.view.LayoutInflater;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.*;
  11. import android.os.Bundle;
  12. import androidx.annotation.NonNull;
  13. import java.util.*;
  14. public class MainActivity extends Activity {
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  20. String[] s = {"全部","美术","体育","音乐"};
  21. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,s);
  22. arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  23. Spinner spinner = findViewById(R.id.spi);
  24. spinner.setAdapter(arrayAdapter);
  25. }
  26. }

十四:列表视图

14.1 xml文件设置列表视图

xml文件

  1. <ListView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:entries="@array/str"
  5. />

values资源文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="str">
  4. <item>全部</item>
  5. <item>电影</item>
  6. <item>图书</item>
  7. <item>游戏</item>
  8. </string-array>
  9. </resources>

14.2 适配器模式设置列表视图

  1. package com.example;
  2. import android.app.Activity;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.os.SystemClock;
  6. import android.view.LayoutInflater;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.*;
  11. import android.os.Bundle;
  12. import androidx.annotation.NonNull;
  13. import java.util.*;
  14. public class MainActivity extends Activity {
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  20. String[] s = {"全部","美术","体育","音乐"};
  21. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,s);
  22. arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  23. ListView listView = findViewById(R.id.img1);
  24. listView.setAdapter(arrayAdapter);
  25. }
  26. }

十五:滚动视图

xml文件垂直滚动

  1. <ScrollView
  2. android:id="@+id/img1"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:textSize="20dp"
  9. android:text="@string/movie"
  10. />
  11. <TextView
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:textSize="20dp"
  15. android:text="@string/music"
  16. />
  17. />

xml文件水平滚动

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:orientation="vertical"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10. <HorizontalScrollView
  11. android:id="@+id/img1"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. <TextView
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:textSize="20dp"
  18. android:text="@string/movie"
  19. />
  20. <TextView
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:textSize="20dp"
  24. android:text="@string/music"
  25. />
  26. />
  27. </LinearLayout>

标签: android

本文转载自: https://blog.csdn.net/qq_56127002/article/details/127499080
版权归原作者 com.fw 所有, 如有侵权,请联系我们删除。

“Android(基本、高级UI组件)”的评论:

还没有评论