主要的用到的控件:HorizontalScrollView
主要的功能:把几张图片解析成一张图片,在一个容器中呈现。
布局文件
side_bar_scollview. //显示view的容器
<? version="1.0" encoding="utf-8"?><LinearLayout ns: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. //显示的主页面
<? version="1.0" encoding="utf-8"?><LinearLayout ns: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. //显示的菜单页面
<? version="1.0" encoding="utf-8"?>< Layout ns: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="菜单" /></ Layout>
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问题解决
-
[python skill]python中tuple 和list 的区别
2026-05-26栏目: 教程
-
[python skill]利用python实现假设性检验方法
2026-05-26栏目: 教程
-
[math skill]Permutation Test 置换检验
2026-05-26栏目: 教程
-
[python skill]利用python计算T分布下的置信区间
2026-05-26栏目: 教程
-
爱了爱了!美到窒息的超美3D作品
2026-05-26栏目: 教程
