Android 高级控件

简介: 高级控件: GridView ScrollView ViewPager SlideMenu PullToRefreshListView 1.ScrollView视图(滚动视图)可以有效的安排这些组件,浏览时可以进行滚屏的操作 垂直滚动——ScrollView 水平滚动——HorizontalScrollView ScrollView是单一容器,只能包含一个组件 练习

高级控件:

GridView

ScrollView
ViewPager
SlideMenu

PullToRefreshListView


1.ScrollView视图(滚动视图)可以有效的安排这些组件,浏览时可以进行滚屏的操作
垂直滚动——ScrollView
水平滚动——HorizontalScrollView
ScrollView是单一容器,只能包含一个组件

练习:设置界面的垂直滚动

                       理想效果                                                         完成效果


布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="设置"
                    android:textSize="30sp"
                    android:layout_marginLeft="100dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="意见反馈"
                    android:layout_gravity="center"
                    android:layout_marginLeft="100dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="列表显示摘要"
                    android:textSize="20sp"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="170dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="字体大小"
                    android:textSize="20sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="中"
                    android:layout_marginLeft="220dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=">"
                    android:textSize="20sp"
                    android:layout_marginLeft="10dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="列表页评论"
                    android:textSize="20sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="不限"
                    android:layout_marginLeft="190dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=">"
                    android:textSize="20sp"
                    android:layout_marginLeft="10dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2G/3G网络流量"
                    android:textSize="20sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="较省流量(智能下图)"
                    android:layout_gravity="center"
                    android:layout_marginLeft="50dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=">"
                    android:textSize="20sp"
                    android:layout_marginLeft="1dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="清理缓存"
                    android:textSize="20sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="当前缓存:2.48MB"
                    android:layout_gravity="center"
                    android:layout_marginLeft="120dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=">"
                    android:textSize="20sp"
                    android:layout_marginLeft="10dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="30dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="推送通知"
                    android:textSize="20sp"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:layout_marginLeft="210dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="互动插件"
                    android:textSize="20sp"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:layout_marginLeft="210dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="自动优化阅读"
                    android:textSize="20sp"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:layout_marginLeft="170dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="收藏时转发"
                    android:textSize="20sp"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="190dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="顶踩时转发"
                    android:textSize="20sp"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="190dp"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#d9d3d3"
                />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>
2.GridView常用属性

android:numColumns=”auto_fit” //GridView的列数设置为自动
android:columnWidth=”90dp " //每列的宽度,也就是Item的宽度
android:stretchMode=”columnWidth“//设置缩放模式,与列宽大小同步
android:verticalSpacing=”10dp” //两行之间的边距
android:horizontalSpacing=”10dp” //两列之间的边距
android:cacheColorHint="#00000000" //去除拖动时默认的黑色背景
android:listSelector="#00000000" //去除选中时的黄色底色
android:scrollbars="none" //隐藏GridView的滚动条

练习:利用GridView完成如下图所示效果

                           理想效果                                                                      完成效果


布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.jreduch05.GridView_Activity">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gv"
        android:numColumns="auto_fit"
        android:columnWidth="100dp"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:cacheColorHint="#00000000"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        >
    </GridView>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/iv"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"
    />
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:gravity="center"
        android:text="测试"
        />
</LinearLayout>

代码:

package com.example.administrator.jreduch05;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridView_Activity extends AppCompatActivity {
    private GridView gv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view_);
        gv=(GridView) findViewById(R.id.gv);
        final List list =new ArrayList();

        Map map=new HashMap();
        map.put("img", R.mipmap.dahai);
        map.put("name", "深海");
        list.add(map);

         map=new HashMap();
        map.put("img",R.mipmap.jietu1);
        map.put("name","深海");
        list.add(map);

         map=new HashMap();
        map.put("img",R.mipmap.dianzan);
        map.put("name","深海");
        list.add(map);

         map=new HashMap();
        map.put("img",R.mipmap.pinglun);
        map.put("name","深海");
        list.add(map);

         map=new HashMap();
        map.put("img",R.mipmap.renrou);
        map.put("name","深海");
        list.add(map);

         map=new HashMap();
        map.put("img",R.mipmap.sannv);
        map.put("name","深海");
        list.add(map);

        map=new HashMap();
        map.put("img",R.mipmap.wuzhi);
        map.put("name","深海");
        list.add(map);

        /*
        1.simpleAdapter 使用的数据源必须继承 Map 接口
        2.from 参数的意思是 指向数据源 Map中的键
        3.to   参数的意思是  为布局中控件ID赋值
         */

        SimpleAdapter sa=new SimpleAdapter(this,list,
                R.layout.gridlayout,
                new String[]{"img","name"},
                new int[]{R.id.iv,R.id.tv}
                );
        gv.setAdapter(sa);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map map=(Map)list.get(position);
                Intent intent=new Intent(GridView_Activity.this,Datu_Activity.class);
                intent.putExtra("img", (int) map.get("img"));
                startActivity(intent);

            }
        });
    }
}

目录
相关文章
|
XML Android开发 数据格式
ConstraintLayout 2,Android高级开发面试
ConstraintLayout 2,Android高级开发面试
|
XML 搜索推荐 Android开发
Android改变进度条控件progressbar的样式(根据源码修改)
本文介绍了如何基于Android源码自定义ProgressBar样式。首先分析了系统源码中ProgressBar样式的定义,发现其依赖一张旋转图片实现动画效果。接着分两步指导开发者实现自定义:1) 模仿源码创建一个旋转动画XML文件(放置在drawable文件夹),修改图片为自定义样式;2) 在UI控件中通过`indeterminateDrawable`属性应用该动画。最终实现简单且个性化的ProgressBar效果,附带效果图展示。
830 2
|
Android开发
Android控件样式的抽取(小提及快捷方式)
在Android开发中,若多个控件样式重复,可抽取公共部分以简化代码。例如对EditText提取样式,通过编辑`styles.xml`实现复用。为提高效率,Android Studio提供自动提取Style功能:右键点击控件样式选项,选择“Style...”,勾选需要提取的属性后确认,即可快速生成样式代码,显著提升开发便利性。
475 2
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
340 3
|
XML 存储 Java
浅谈Android的TextView控件
浅谈Android的TextView控件
317 0
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
813 4
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
1175 3
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
1207 2
|
架构师 网络协议 算法
Android高级架构师整理面试经历发现?(大厂面经+学习笔记(1)
Android高级架构师整理面试经历发现?(大厂面经+学习笔记(1)
|
Android开发
Android高级开发面试题以及笞案整理,实战解析
Android高级开发面试题以及笞案整理,实战解析

热门文章

最新文章