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