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

            }
        });
    }
}

目录
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
42 1
|
4月前
|
Java Android开发
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
174 0
|
17天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
1月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
12 0
|
3月前
|
Android开发
分享88个Android控件源代码总有一个是你想要的
分享88个Android控件源代码总有一个是你想要的
23 0
|
3月前
|
Android开发
分享89个Android控件源代码总有一个是你想要的
分享89个Android控件源代码总有一个是你想要的
73 0
|
4月前
|
XML Android开发 数据格式
[Android]开关控件Switch
[Android]开关控件Switch
36 0
|
4月前
|
Android开发 Kotlin
android开发,使用kotlin学习滚动控件RecyclerView
android开发,使用kotlin学习滚动控件RecyclerView
38 0
|
4月前
|
XML Java Android开发
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
72 0
|
8月前
|
缓存 Java API
#7,Android开发 控件 ProgressBar 进度条
#7,Android开发 控件 ProgressBar 进度条