Android实现顶部导航菜单左右滑动效果

2019-12-10 17:53:16刘景俊

为了实现导航菜单上的左右图片,需要在main.xml布局文件中设置相对布局。

Android,导航菜单,左右滑动

这个示例中,是把左右导航的图片显示在文字上方,在点击上图中右三角图片时会显示下一个页面导航,具体大家可以看下面代码。 
main.xml中设置左右图片的相对布局代码:
        

 <RelativeLayout 
       android:id="@+id/linearLayout01"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >

 <android.support.v4.view.ViewPager
 android:id="@+id/slideMenu"
 android:layout_width="fill_parent"
 android:layout_height="35dp"
 android:background="@drawable/top_bg" />

 <RelativeLayout
 android:id="@+id/linearLayout01"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >
 

 <ImageView
  android:id="@+id/ivPreviousButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:paddingTop="10dp"
  android:paddingLeft="5dp"
  android:visibility="invisible"
  android:src="@drawable/previous_button" />
 
 </RelativeLayout>
 
 <RelativeLayout
 android:id="@+id/linearLayout01"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >
 
 <ImageView
  android:id="@+id/ivNextButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:paddingTop="10dp"
  android:paddingRight="5dp"
  android:visibility="invisible"
  android:src="@drawable/next_button" />

 <!-- 
 <ImageView
  android:id="@+id/ivMenuBackgroundCopy"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:paddingTop="2dp"
  android:src="@drawable/menu_bg" />
  -->
 
 </RelativeLayout>

 <ImageView
 android:id="@+id/ivMenuBackground"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerVertical="true"
 android:layout_marginLeft="2dp"
 android:layout_toRightOf="@+id/ivPreviousButton"
 android:paddingTop="2dp"
 android:visibility="gone"
 android:src="@drawable/menu_bg" />
 </RelativeLayout>