zoukankan      html  css  js  c++  java
  • 【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);
                    
                }
            });
            
        }
    
    }

    效果图:

  • 相关阅读:
    一种字段串的拆分
    获取存储过程返回值的几种方式
    使用表值参数(数据库引擎)
    转 Dynamics CRM Alert and Notification JavaScript Methods
    CRM 2016 自定义对话框
    CRM 2016 子表单中N:1关系 字段要求与新建时的关系
    恢复CRM plugin
    CRM Diagnostics CRM 2016 诊断
    [Windows] Visual Studio 2010 快捷键大全
    [Java] Web开发HTTP状态码整理
  • 原文地址:https://www.cnblogs.com/lossingdawn/p/4466281.html
Copyright © 2011-2022 走看看