【Android UI】侧滑栏的使用(HorizontalScrollView控件的使用)
主要的用到的控件:HorizontalScrollView
主要的功能:把几张图片解析成一张图片,在一个容器中呈现。
布局文件xml
side_bar_scollview.xml//显示view的容器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <HorizontalScrollView
android:id="@+id/MyScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:id="@+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout> </HorizontalScrollView> </LinearLayout>
home.xml//显示的主页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/home_bg"> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主页" /> </LinearLayout>
menu.xml//显示的菜单页面
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu_bg"> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜单" /> </FrameLayout>
MainActivity.java//主活动
package com.example.side_bar_scrollview; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout; public class MainActivity extends Activity { private HorizontalScrollView scrollview;
private LinearLayout view_layout;
private int width;
private int height;
private View home_view;
private View menu_view;
private float rate=0.4f; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//关联界面ID
setContentView(R.layout.side_bar_scollview);
//关联控件ID
scrollview=(HorizontalScrollView) findViewById(R.id.MyScrollView);
view_layout=(LinearLayout) findViewById(R.id.ll_layout);
//监听布局
MyLayoutListener();
//隐藏滚动条
scrollview.setHorizontalScrollBarEnabled(false);
} /**
* 监听布局的变化
* 1.getViewTreeObserver --- view事件的观察者
* 2.addOnGlobalLayoutListener
* 当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时,
* 所要调用的回调函数的接口类
*
*/
private void MyLayoutListener(){ scrollview.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() { @Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
//移除之前已经注册的全局布局的回调函数,使图片不会循环连在一起
view_layout.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
//获取最后一次调用measure()测量得到的scrollview的宽和高
height = scrollview.getMeasuredHeight();
width = scrollview.getMeasuredWidth();
//解析主页和菜单的布局
home_view=getLayoutInflater().inflate(R.layout.home,
null);
menu_view=getLayoutInflater().inflate(R.layout.menu,
null);
//添加view到view_layout
view_layout.addView(menu_view, (int)(width*rate), height);
view_layout.addView(home_view, width, height); }
}); } }
效果图:


【Android UI】侧滑栏的使用(HorizontalScrollView控件的使用)的更多相关文章
- Android自定义控件View(三)组合控件
不少人应该见过小米手机系统音量控制UI,一个圆形带动画效果的音量加减UI,效果很好看.它是怎么实现的呢?这篇博客来揭开它的神秘面纱.先上效果图 相信很多人都知道Android自定义控件的三种方式,An ...
- Android下拉涮新第三方通用控件
Android下拉涮新第三方通用控件https://github.com/chrisbanes/Android-PullToRefresh Pull To Refresh Views for Andr ...
- WPF里面多线程访问UI线程、主线程的控件
如果出现以下错误:调用线程无法访问此对象,因为另一个线程拥有该对象. 你就碰到多线程访问UI线程.主线程的控件的问题了. 先占位.
- Android 图片混排富文本编辑器控件
概述 一个Android 图片混排富文本编辑器控件(仿兴趣部落) 详细 代码下载:http://www.demodashi.com/demo/12032.html 一.一个Android 图片混排富文 ...
- Android音乐、视频类APP常用控件:DraggablePanel(2)
Android音乐.视频类APP常用控件:DraggablePanel(2) 附录文章1主要演示了如何使用DraggablePanel 的DraggableView.DraggablePanel ...
- Android音乐、视频类APP常用控件:DraggablePanel(1)
Android音乐.视频类APP常用控件:DraggablePanel(1) Android的音乐视频类APP开发中,常涉及到用户拖曳视频.音乐播放器产生一定交互响应的设计需求,最典型的以You ...
- 【Android】11.0 UI开发(二)——列表控件ListView的简单实现1
************************ 转载请注明出处:https://www.cnblogs.com/xiaofu007/p/10342462.html ***************** ...
- 【Android】15.0 UI开发(六)——列表控件RecyclerView的网格布局排列实现
1.0 列表控件RecyclerView的网格布局排列实现,关键词GridLayoutManager. LinearLayoutManager 实现顺序布局 GridLayoutManager 实现网 ...
- 【Android】14.0 UI开发(五)——列表控件RecyclerView的瀑布布局排列实现
1.0 列表控件RecyclerView的瀑布布局排列实现,关键词StaggeredGridLayoutManager LinearLayoutManager 实现顺序布局 GridLayoutMan ...
随机推荐
- 批处理(bat)实现SQLServer数据库备份与还原
原文:批处理(bat)实现SQLServer数据库备份与还原 备份数据库.bat @echo off set path=%path%;C:\Program Files (x86)\Microsoft ...
- DIXML(包括所有的W3C XML标准)
Description:DIXml is an embedded XML, XSLT, and EXSLT processing library for Delphi (Embarcadero / C ...
- Decision Tree
Decision Tree builds classification or regression models in the form of a tree structure. It break d ...
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
这是我为项目中写的一个测试的例子, 假如,您需要这样一个xml文件, <?xml version="1.0" encoding="utf-8"?> ...
- .NET错误:未找到类型或命名空间名称
现象:编译项目时提示未找到类型或命名空间名称"... " 解决方法:如果是未找到类型,检查是否引用了类型所在的命名空间,使用using指令:如果是未找到命名空间,那么检查是否引用了 ...
- Spring AOP APIS
1:Pointcut API in Spring (1):切点接口定义 org.springframework.aop.Pointcut接口是中心接口.用来将Advice(通知)定位到特定的类和方法. ...
- 解码mmo游戏服务器三:大地图同步(aoi)
问题引入:aoi(area of interest).在大地图中,玩家只需要关心自己周围的对象变化,而不需要关心距离较远的对象的变化.所以大地图中的数据不需要全部广播,只要同步玩家自己视野范围的消息即 ...
- kubernetes实战篇之windows添加自签ca证书信任
系列目录 由于服务端设置了https访问,因此如果通过浏览器访问时会提示证书不被信任,但是仍然可以通过处理继续访问.但是在自动化环境中,都是通过命令来请求的,这样不受信任的https就会报错误,这样我 ...
- Spring Boot:实现MyBatis动态数据源
综合概述 在很多具体应用场景中,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动态数据 ...
- spring 5.x 系列第22篇 —— spring 定时任务 (代码配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 关于任务的调度配置定义在ServletCo ...