Android高级UI组件

心已赠人 2023-07-03 10:33 88阅读 0赞

Android高级UI组件

  1. 进度条类组件
  2. 图像类组件
  3. 列表类组件
  4. 通用组件

一、进度条

用ProgressBar来标记。

1.Andorid提供了水平进度条、圆形进度条,不加设置默认为圆形进度条。

想展示实时进度:使用水平进度条
只想显示在加载:圆形进度条

2.属性:
①style来修改进度条的样式:特别注意无前缀andorid
style的属性值:
?android:attr/progressBarStyleHorizontal,细水平长条进度条
?android:attr/progressBarStyleSmall,小圆形进度条
?android:attr/progressBarStyleLarge,大圆形进度条
@android:style/Widget.ProgressBar.Horizontal,粗水平长条进度条
@android:style/Widget.ProgressBar.Large,旋转画面的大圆形进度条
@android:style/Widget.ProgressBar.Small,旋转画面的小圆形进度条

②android:max,设置最大值,属性值可为具体数值。

③android:progress,设置当前进度,属性值为具体数值

3.事件监听器

实现进度条的实时改变:需创建线程,在线程中循环获取耗时的进度,并更新进度。
在这里插入图片描述
在Android中不支持在主线程中更新UI组件,可以实例化一个Handler对象,即消息处理对象,通过发送消息来更新UI组件。、

对于耗时操作可通过一个线程来模拟

  1. progressBar= (ProgressBar) findViewById(R.id.pg);
  2. //消息处理
  3. mHandler=new Handler() {
  4. @Override
  5. public void handleMessage(Message msg) {
  6. if(msg.what==0x111){
  7. progressBar.setProgress(mProgress);
  8. }
  9. else
  10. {
  11. Toast.makeText(MainActivity.this, "耗时操作已完成", Toast.LENGTH_SHORT).show();
  12. progressBar.setVisibility(View.GONE);
  13. }
  14. }
  15. };
  16. //创建一个线程,模拟耗时操作
  17. new Thread(new Runnable() {
  18. @Override
  19. public void run() {
  20. while(true)
  21. {
  22. mProgress=doWork();
  23. Message m=new Message();
  24. if(mProgress<100){
  25. m.what=0x111;//耗时操作未完成,为自定义代码,一般以0x开头
  26. mHandler.sendMessage(m);
  27. }
  28. else
  29. {
  30. m.what=0x110;//耗时操作已完成
  31. mHandler.sendMessage(m);
  32. break;
  33. }
  34. }
  35. }
  36. private int doWork()
  37. {
  38. mProgress+=Math.random()*10;
  39. try {
  40. Thread.sleep(200);
  41. } catch (InterruptedException e) {
  42. e.printStackTrace();
  43. }
  44. return mProgress;
  45. }
  46. }).start();//开始线程
  47. }

二、拖动条

1.用SeekBar来标记。
2.为进度条的子类,具有进度条拥有的属性。
3.属性:
①android:thumb,修改拖动条的小点,属性值可为图片资源
②android:max,设置最大值,属性值可为具体数值。
③android:progress,设置当前进度,属性值为具体数值

三、星级评分条

用RatingBar标记,默认星星为灰色的

1.属性
①android:numStars,设置评分条的星星个数
②android:rating,属性值为数值,设置初始点亮个数
③android:stepSize,设置最小点亮星数。
④android:isIndicator,属性值为布尔类型,为true时,评分条只能看不能改

四、图像视图

用来显示图片,用ImageView来添加。

为图像组件,属性:
①android:src,显示图片

②设置缩放方式
android:scaleType=“fitXY” //把图片按照指定的大小在ImageView中显示,拉伸显示图片,不保持原比例,填满ImageView.
android:scaleType=“fitStart”//把原图按照比例放大缩小到ImageView的高度,显示在ImageView的start(前部/上部)

③设置最大宽度、高度:
android:adjustViewBounds=“true”//可设置最大高度,宽度…
android:maxWidth=“100dp”
android:maxHeight=“100dp”

④设置图片着色
android:tint,

在这里插入图片描述

  1. 扩展:只允许横屏模式,
  2. 则在manifestsactivity后添加android:screenOrientation="landscape">

五、图像切换器

实现带动画效果的图片切换功能。

使用ImageSwitcher来添加。

设置初始图片:

  1. imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
  2. @Override
  3. public View makeView() {
  4. //创建一个图片对象
  5. ImageView imageView=new ImageView(MainActivity.this);
  6. //给该对象设置图片
  7. imageView.setImageResource(arrayPicture[index]);
  8. return imageView;
  9. }
  10. });

使用Java代码来实现切换动画效果

  1. ImageSwitcher imageSwitcher
  2. imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
  3. android.R.anim.fade_out));//设置淡出

六、网格视图

按行列分布方式显示多个组件

用GridView添加网格视图。

要显示图片,要设置一个适配器(Adapter),适配器:连接后端数据与前端显示的接口,是数据和UI组件中的重要纽带。要添加适配器,要在Java文件中写代码

四个常用适配器:
ArrayAdapter,数组适配器,将数组的多个值包装成列表项,只能显示一行文字。
SmipleAdapter,把List的多个值包装成多个列表项。
SmipleCursorAdapter,将数据库的内容以列表的形式展示出来。
BaseAdapter,对各个列表项进行最大限度的定制。

属性:
android:numColumns,设置列数,属性值为数值或auto_fit(自动排列)。

  1. **MainActivity.java**
  2. package com.mingrisoft.qqal;
  3. import android.content.Context;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.GridView;
  10. import android.widget.ImageButton;
  11. import android.widget.ImageView;
  12. public class MainActivity extends ActionBarActivity {
  13. //放置图片资源id的int数组。
  14. private int[] picture=new int[]{
  15. R.drawable.t1,R.drawable.t2,R.drawable.t3,R.drawable.t4,
  16. R.drawable.t5,R.drawable.t6,R.drawable.t7,R.drawable.t8,
  17. R.drawable.t9
  18. };
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. //获取网格视图
  24. GridView gridView= (GridView) findViewById(R.id.gridview);
  25. //将适配器加入到网格视图中
  26. gridView.setAdapter(new ImageAdpter(this));
  27. }
  28. //自定义一个适配器,继承自BaseAdapter
  29. public class ImageAdpter extends BaseAdapter{
  30. //上下文对象
  31. private Context mContext;
  32. public ImageAdpter(Context c){
  33. mContext=c;
  34. }
  35. @Override
  36. public int getCount() {
  37. return picture.length;
  38. }
  39. @Override
  40. public Object getItem(int i) {
  41. return null;
  42. }
  43. @Override
  44. public long getItemId(int i) {
  45. return 0;
  46. }
  47. @Override
  48. public View getView(int i, View view, ViewGroup viewGroup) {
  49. ImageView imageView;
  50. if(view==null)
  51. {
  52. imageView=new ImageView(mContext);
  53. //设置大小
  54. imageView.setLayoutParams(new GridView.LayoutParams(100,90));
  55. //设置显示形式
  56. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  57. }
  58. else
  59. {
  60. imageView=(ImageView)view;
  61. }
  62. imageView.setImageResource(picture[i]);
  63. return imageView;
  64. }
  65. }
  66. }
  67. **layout.xml文件**
  68. <?xml version="1.0" encoding="utf-8"?>
  69. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  70. xmlns:tools="http://schemas.android.com/tools"
  71. android:layout_width="match_parent"
  72. android:layout_height="match_parent"
  73. android:orientation="vertical"
  74. android:paddingBottom="@dimen/activity_vertical_margin"
  75. android:paddingLeft="@dimen/activity_horizontal_margin"
  76. android:paddingRight="@dimen/activity_horizontal_margin"
  77. android:paddingTop="@dimen/activity_vertical_margin"
  78. tools:context="com.mingrisoft.qqal.MainActivity">
  79. <TextView
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content"
  82. android:text="2020年2月7日" />
  83. <GridView
  84. android:id="@+id/gridview"
  85. android:layout_width="match_parent"
  86. android:layout_height="match_parent"
  87. android:numColumns="auto_fit"
  88. android:layout_gravity="center"
  89. android:verticalSpacing="5dp"
  90. android:columnWidth="100dp"></GridView>
  91. </LinearLayout>

七、下拉列表框

使用Spinner添加

属性:
android:entries,添加下拉列表项,属性值为数组资源。
下拉列表框可使用entries来设置或使用适配器来指定。
一般使用适配器

1、使用适配器:

  1. //字符串数组,数据源
  2. String[] ctype=new String[]{ "全部","美术","音乐","体育"};
  3. //设置常量(final)数组适配器,内含元素为字符串,第一个参数this为上下文指针
  4. // 第二个参数android.R.layout.simple_spinner_item为android提供的外观形式,第三个参数为显示文字
  5. final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ctype);
  6. //设置下拉菜单样式,android.R.layout.simple_spinner_dropdown_item为android提供的外观样式
  7. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  8. //获取下拉列表框
  9. Spinner spinner= (Spinner) findViewById(R.id.spinner);
  10. //将ArrayAdapter 添加Spinner 对象中
  11. spinner.setAdapter(adapter);
  12. //事件监听器,此时为:项目被选择时监听器
  13. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  14. @Override
  15. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  16. //获取选中值,并将其变成字符串。
  17. String str = adapterView.getItemAtPosition(i).toString();
  18. //输出
  19. Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
  20. }
  21. @Override
  22. public void onNothingSelected(AdapterView<?> adapterView) {
  23. }
  24. });
  25. }

2、使用entries

  1. android:entries="@array/ctype"
  2. //在value中新建一个array.xml
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <resources>
  5. <string-array name="ctype">
  6. <item>全部</item>
  7. <item>电影/电视</item>
  8. <item>图书</item>
  9. <item>唱片</item>
  10. <item>小事</item>
  11. <item>用户</item>
  12. <item>小组</item>
  13. <item>群聊</item>
  14. <item>游戏/应用</item>
  15. <item>活动</item>
  16. </string-array>
  17. </resources>

特别注意:在下拉列表中,设置的是选择事件监听器,在列表视图中是点击事件监听器!!

八、列表视图

用ListView来标记。
列表项可使用entries来设置或使用适配器来指定。

使用适配器

  1. //字符串数组
  2. String[] ctype=new String[]{ "全部","图书","游戏","电视"};
  3. //设置适配器,元素为String、
  4. ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ctype);
  5. //获取列表视图
  6. ListView listView= (ListView) findViewById(R.id.listview);
  7. //将适配器放入列表视图
  8. listView.setAdapter(adapter);
  9. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  10. @Override
  11. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  12. String str=adapterView.getItemAtPosition(i).toString();
  13. Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
  14. }
  15. });

九、滚动视图

使用ScrollView/HorizontalScrollView来添加。
其中,HorizontalScrollView为水平滚动条,ScrollView为竖直滚动条。

特别注意,一个滚动视图中只能添加一个组件,若想放置多个,需用一个布局管理器括起来
添加滚动视图的两种方法:
1、在xml文件中添加
2、在Java文件中添加

在xml中添加

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.mingrisoft.demo.MainActivity">
  11. <HorizontalScrollView
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:orientation="vertical">
  18. <TextView
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:textSize="50sp"
  22. android:text="@string/content"/>
  23. <TextView
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent"
  26. android:textSize="50sp"
  27. android:text="@string/content"/>
  28. </LinearLayout>
  29. </HorizontalScrollView>
  30. </RelativeLayout>

在Java文件中添加

  1. 使用构造方法ScrollView(Context c)创建一个滚动式图
  2. 应用addView()方法添加组件到滚动视图中
  3. 将滚动视图添加到布局管理器中。

例子如下:
acitivity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/ll"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:paddingLeft="@dimen/activity_horizontal_margin"
  10. android:paddingRight="@dimen/activity_horizontal_margin"
  11. android:paddingTop="@dimen/activity_vertical_margin"
  12. tools:context="com.mingrisoft.javascrollview.MainActivity">
  13. </LinearLayout>

Activity_main.java文件

  1. package com.mingrisoft.javascrollview;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.os.Bundle;
  4. import android.widget.ImageView;
  5. import android.widget.LinearLayout;
  6. import android.widget.ScrollView;
  7. import android.widget.TextView;
  8. public class MainActivity extends ActionBarActivity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. //获得xml中的根布局管理器
  14. LinearLayout ll= (LinearLayout) findViewById(R.id.ll);
  15. //创建一个布局管理器对象,用来将两个组件(ImageView和TextView)括起来
  16. LinearLayout ll2=new LinearLayout(MainActivity.this);
  17. //设置为垂直线性
  18. ll2.setOrientation(LinearLayout.VERTICAL);
  19. //创建滚动视图
  20. ScrollView scrollView=new ScrollView(MainActivity.this);
  21. //把滚动视图放入根布局管理器
  22. ll.addView(scrollView);
  23. //将垂直线性管理器放入根布局管理器
  24. scrollView.addView(ll2);
  25. //创建图像视图并添加
  26. ImageView imageView=new ImageView(MainActivity.this);
  27. imageView.setImageResource(R.drawable.tt1);
  28. ll2.addView(imageView);
  29. //创建文本框并添加
  30. TextView textView=new TextView(MainActivity.this);
  31. textView.setText(R.string.context);
  32. ll2.addView(textView);
  33. }
  34. }

十、选项卡

多标签页界面
作用:使界面简洁大方,减少窗体个数。

如何添加,使用选项卡?
不能通过具体组件在xml文件中直接添加,需4个步骤。

  1. 在布局文件中添加TabHost、TabWidget、TabContent组件。前两个在布局文件中有具体标记,后一个需要FrameLayout来实现。
  2. 编写各标签页的XML布局文件,即有几个标签页就编写几个布局文件,布局文件中的内容即为选项卡中显示的内容。
  3. 获取并初始化TabHost组件。
  4. 为TabHost对象添加标签页

实例:
在这里插入图片描述
在这里插入图片描述

activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@android:id/tabhost"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.mingrisoft.demo2.MainActivity">
  8. <!--因为要添加两个组件,所以要用LinearLayout括起来 -->
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical">
  13. <!-- id为android预定好的id -->
  14. <TabWidget
  15. android:id="@android:id/tabs"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. ></TabWidget>
  19. <!-- TabContent-->
  20. <FrameLayout
  21. android:id="@android:id/tabcontent"
  22. android:layout_width="match_parent"
  23. android:layout_height="match_parent">
  24. </FrameLayout>
  25. </LinearLayout>
  26. </TabHost>

设计视图:其中上面的TAB LABEL为添加的TabWeiget,下面为FrameLayout
在这里插入图片描述

tab1.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@+id/left"
  6. android:orientation="vertical">
  7. <ImageView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:src="@drawable/s1"/>
  11. </LinearLayout>

tab2.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@+id/right"
  6. android:orientation="vertical">
  7. <ImageView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:src="@drawable/s2"/>
  11. </LinearLayout>

MainActivity.java文件

  1. package com.mingrisoft.demo2;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.widget.TabHost;
  6. public class MainActivity extends ActionBarActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. //获取xml中TabHost对象
  12. TabHost tabHost= (TabHost) findViewById(android.R.id.tabhost);
  13. //对组件初始化
  14. tabHost.setup();
  15. //添加2个标签页,需声明并实例化LayoutInflater
  16. LayoutInflater inflater=LayoutInflater.from(this);
  17. inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
  18. inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
  19. //addTab为添加标签页,newTabSpec指定标签页,,setIndicator指定文字,setContent指定内容
  20. tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("文字").setContent(R.id.left));
  21. tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("主题").setContent(R.id.right));
  22. }
  23. }

发表评论

表情:
评论列表 (有 0 条评论,88人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android UI

    基本组件: 1. 文本类组件 2. 按钮类组件 3. 日期时间类组件 一、文本框组件 文本框文字默认自动换行显示,若不想让其换行,可使用android:sing