【Android UI】側滑欄的使用

來源:互聯網
上載者:User

標籤:

布局檔案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】側滑欄的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.