欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義HorizontalScrollView實(shí)現(xiàn)qq側(cè)滑菜單

 更新時(shí)間:2017年04月05日 09:31:42   作者:甄情  
本文主要介紹了android自定義HorizontalScrollView實(shí)現(xiàn)qq側(cè)滑菜單的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

今天看了鴻洋_大神在慕課網(wǎng)講的qq5.0側(cè)滑菜單。學(xué)了不少的知識(shí),同時(shí)也佩服鴻洋_大神思路的清晰。

看了教程課下也自己實(shí)現(xiàn)了一下。代碼幾乎完全相同  別噴我啊。。沒(méi)辦法 o(︶︿︶)o 唉

像素不好 沒(méi)辦法 找不到好的制作gif的軟件。

我們暫且稱側(cè)滑左邊界面的為menu,右邊為content

首先是menu的布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:background="@drawable/img_frame_background" > 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_centerInParent="true" 
 android:orientation="vertical" > 
 <RelativeLayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" > 
 <ImageView 
 android:id="@+id/image1" 
 android:layout_width="50dp" 
 android:layout_height="50dp" 
 android:layout_marginLeft="20dp" 
 android:src="@drawable/img_1" /> 
 <TextView 
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
android:layout_toRightOf="@id/image1" 
 android:textColor="#ffffff" 
 android:layout_marginLeft="20dp" 
 android:text="第一個(gè)Item" 
 android:textSize="20sp" /> 
 </RelativeLayout> 
 <RelativeLayout 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" > 
 <ImageView 
 android:id="@+id/image2" 
 android:layout_width="50dp" 
 android:layout_height="50dp" 
 android:layout_marginLeft="20dp" 
 android:src="@drawable/img_2" /> 
 <TextView 
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
android:layout_toRightOf="@id/image2" 
 android:textColor="#ffffff" 
 android:layout_marginLeft="20dp" 
 android:text="第二個(gè)Item" 
 android:textSize="20sp" /> 
 </RelativeLayout> 
 <RelativeLayout 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" > 
 <ImageView 
 android:id="@+id/image3" 
 android:layout_width="50dp" 
 android:layout_height="50dp" 
 android:layout_marginLeft="20dp" 
 android:src="@drawable/img_3" /> 
 <TextView 
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
android:layout_toRightOf="@id/image3" 
 android:textColor="#ffffff" 
 android:layout_marginLeft="20dp" 
 android:text="第三個(gè)Item" 
 android:textSize="20sp" /> 
 </RelativeLayout> 
 <RelativeLayout 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" > 
 <ImageView 
 android:id="@+id/image4" 
 android:layout_width="50dp" 
 android:layout_height="50dp" 
 android:layout_marginLeft="20dp" 
 android:src="@drawable/img_4" /> 
 <TextView 
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
android:layout_toRightOf="@id/image4" 
 android:textColor="#ffffff" 
 android:layout_marginLeft="20dp" 
 android:text="第四個(gè)Item" 
 android:textSize="20sp" /> 
 </RelativeLayout> 
<RelativeLayout 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" > 
<ImageView 
 android:id="@+id/image5" 
 android:layout_width="50dp" 
 android:layout_height="50dp" 
 android:layout_marginLeft="20dp" 
 android:src="@drawable/img_5" /> 
<TextView 
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
android:layout_toRightOf="@id/image5" 
 android:textColor="#ffffff" 
 android:layout_marginLeft="20dp" 
 android:text="第五個(gè)Item" 
 android:textSize="20sp" /> 
 </RelativeLayout> 
 </LinearLayout> 
</RelativeLayout> 

然后是主布局,一個(gè)水平滾動(dòng)條,放入menu.xml,然后下面是一個(gè)線性垂直布局,背景是qq圖片

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" > 
<com.example.myhorizontalscrollview.MyHorizontalScrollView 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:scrollbars="none"> 
 <LinearLayout 
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent" 
 android:orientation="horizontal" 
 > 
 <include layout="@layout/menu" /> 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
android:background="@drawable/qq" /> 
 </LinearLayout> </com.example.myhorizontalscrollview.MyHorizontalScrollView> 
</RelativeLayout> 

其中的水平滾動(dòng)條是我們自定義的view

需要重寫(xiě)其中的兩個(gè)方法

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
 // TODO Auto-generated method stub 
 super.onLayout(changed, l, t, r, b); 
} 

通過(guò)設(shè)置偏移量,調(diào)整我們的初始布局,使menu全部隱藏,右側(cè)菜單顯現(xiàn)

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 // TODO Auto-generated method stub 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

設(shè)置子view的寬

     * 因?yàn)镠orizontalScrollView自己控制move和down的事件

     * 所以我們還要通過(guò)onTouchEvent判斷一下up.如果當(dāng)前的x偏移量大于menu寬度的一半

     * 隱藏,否則顯示menu 顯示的時(shí)候通過(guò)smoothScrollTo(x, y)方法來(lái)實(shí)現(xiàn)動(dòng)畫(huà)的效果

下面是所有的自定義的HorizontalScrollView

package com.example.myhorizontalscrollview; 
import android.annotation.SuppressLint; 
import android.content.Context; 
import android.text.GetChars; 
import android.util.AttributeSet; 
import android.util.DisplayMetrics; 
import android.util.TypedValue; 
import android.view.MotionEvent; 
import android.view.ViewGroup; 
import android.view.WindowManager; 
import android.widget.HorizontalScrollView; 
import android.widget.LinearLayout; 
public class MyHorizontalScrollView extends HorizontalScrollView { 
 //滾動(dòng)條中的水平先行布局 
 private LinearLayout mWrpper; 
 //水平線性布局的左側(cè)菜單menu 
 private ViewGroup mMenu; 
 //水平先行布局的右側(cè)線性布局 
 private ViewGroup mContent; 
 //屏幕的寬 
 private int mScreenWidth; 
 //menu的寬離屏幕右側(cè)的距離 
 private int mMenuRightPadding=50; 
 //menu的寬度 
 private int mMenuWidth; 
 private boolean once; 

 /** 
 * 未使用自定義屬性時(shí)調(diào)用 
 * */ 
 public MyHorizontalScrollView(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 /* 
 * 獲取屏幕的寬度 
 * 通過(guò)context拿到windowManager,在通過(guò)windowManager拿到Metrics,用DisplayMetrics接收 
 * */ 
 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
 DisplayMetrics outMetrics = new DisplayMetrics(); 
 wm.getDefaultDisplay().getMetrics(outMetrics); 
 mScreenWidth=outMetrics.widthPixels; 
 //把dp轉(zhuǎn)換成px 
 mMenuRightPadding=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, 
 context.getResources().getDisplayMetrics()); 
 } 
 /* 
 * 設(shè)置子view的寬和高 
 * */ 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 // TODO Auto-generated method stub 
 if(!once){ 
 mWrpper=(LinearLayout) getChildAt(0); 
 mMenu=(ViewGroup) mWrpper.getChildAt(0); 
 mContent=(ViewGroup) mWrpper.getChildAt(1); 
 //menu的寬度等于屏幕的寬度減去menu離屏幕右側(cè)的邊距 
 mMenuWidth=mMenu.getLayoutParams().width=mScreenWidth-mMenuRightPadding; 
 //右邊的先行布局的寬度直接等于屏幕的寬度 
 mContent.getLayoutParams().width=mScreenWidth; 
 once=true; 
 } 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 } 
 /* 
 * 通過(guò)設(shè)置偏移量將menu隱藏 
 * */ 
 @Override 
 protected void onLayout(boolean changed, int l, int t, int r, int b) { 
 // TODO Auto-generated method stub 
 super.onLayout(changed, l, t, r, b); 
 /* 
 * 通過(guò)scrollTo(x,y)方法設(shè)置屏幕的偏移量,x為正 
 * 內(nèi)容向左移動(dòng) 
 * */ 
 if(changed){ 
 this.scrollTo(mMenuWidth, 0); 
 } 
 } 
 /* 
 * 因?yàn)镠orizontalScrollView自己控制move和down的事件 
 * 所以我們還要判斷一下up.如果當(dāng)前的x偏移量大于menu寬度的一半 
 * 隱藏,否則顯示menu 
 * */ 
 @Override 
 public boolean onTouchEvent(MotionEvent ev) { 
 // TODO Auto-generated method stub 
 int action=ev.getAction(); 
 switch(action){ 
 case MotionEvent.ACTION_UP: 
 int scrollX=getScrollX(); 
 if(scrollX>=mMenuWidth/2){ 
 this.smoothScrollTo(mMenuWidth, 0); 
 } 
 else{ 
 this.smoothScrollTo(0, 0); 
 } 
 return true; 
 } 
 return super.onTouchEvent(ev); 
 } 
} 

然后就是MainActivity加載布局就可以

package com.example.slipping; 
import com.example.helloworld.R; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
public class MainActivity extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 } 
} 

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • Android RadioGroup 設(shè)置某一個(gè)選中或者不可選中的方法

    Android RadioGroup 設(shè)置某一個(gè)選中或者不可選中的方法

    下面小編就為大家?guī)?lái)一篇Android RadioGroup 設(shè)置某一個(gè)選中或者不可選中的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼

    Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼

    本篇文章主要介紹了Android實(shí)現(xiàn)定時(shí)器Timer的停止和重啟實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法

    Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法

    這篇文章主要給大家介紹了關(guān)于Android如何將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法,文中通過(guò)示例代碼介紹的非常吸納關(guān)系,對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android convinientbanner頂部廣告輪播控件使用詳解

    Android convinientbanner頂部廣告輪播控件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android convinientbanner頂部廣告輪播控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android4.X中SIM卡信息初始化過(guò)程詳解

    Android4.X中SIM卡信息初始化過(guò)程詳解

    這篇文章主要介紹了Android4.X中SIM卡信息初始化過(guò)程,較為詳細(xì)的分析總結(jié)了Android4.X中SIM卡信息初始化的步驟及相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Kotlin Channel處理多個(gè)數(shù)據(jù)組合的流

    Kotlin Channel處理多個(gè)數(shù)據(jù)組合的流

    最近項(xiàng)目中對(duì) kotlin 的使用比較多。不得不說(shuō) kotlin 確實(shí)可以極大的提高 android 的開(kāi)發(fā)效率,channel用于協(xié)程之間的通訊,使用send和receive往通道里寫(xiě)入或者讀取數(shù)據(jù),2個(gè)方法為非阻塞掛起函數(shù),channel是熱流,不管有沒(méi)有訂閱者都會(huì)發(fā)送
    2022-11-11
  • 實(shí)例詳解用戶輸入 i. 檢測(cè)常用手勢(shì)

    實(shí)例詳解用戶輸入 i. 檢測(cè)常用手勢(shì)

    通過(guò)本段代碼給大家介紹當(dāng)用戶輸入i檢測(cè)常用手勢(shì)的相關(guān)內(nèi)容,代碼簡(jiǎn)單易懂,感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android源碼解析之截屏事件流程

    Android源碼解析之截屏事件流程

    這篇文章我們主要講一下Android系統(tǒng)中的截屏事件處理流程。用過(guò)android系統(tǒng)手機(jī)的同學(xué)應(yīng)該都知道,一般的android手機(jī)按下音量減少鍵和電源按鍵就會(huì)觸發(fā)截屏事件.
    2018-05-05
  • Android使用getIdentifier()獲取資源Id的方法

    Android使用getIdentifier()獲取資源Id的方法

    這篇文章主要介紹了Android使用getIdentifier()獲取資源Id的方法,涉及Android針對(duì)控件資源的相關(guān)操作技巧,需要的朋友可以參考下
    2016-08-08
  • Android組件實(shí)現(xiàn)列表選擇框功能

    Android組件實(shí)現(xiàn)列表選擇框功能

    android提供的列表選擇框(Spinner)相當(dāng)于web端用戶注冊(cè)時(shí)的選擇下拉框,比如注冊(cè)候選擇省份城市等。這篇文章主要介紹了Android組件實(shí)現(xiàn)列表選擇框功能,需要的朋友可以參考下
    2017-02-02

最新評(píng)論