android实现类似淘宝的底部滑动导航菜单

1、首先创建layout文件,代码如下:

<?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" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff3390"
        >
        
    </LinearLayout>


    
    <LinearLayout
        android:id="@+id/testLl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#eeead6"
     >
<!-- 登录 -->
<Button 
  android:id="@+id/mainNavbar_loginBt"
       android:layout_width="50dp"      
       android:layout_height="match_parent"
       android:layout_marginTop="10dp"
       android:layout_marginBottom="10dp"
       android:layout_marginLeft="5dp"
       android:layout_marginRight="5dp"
       android:textColor="#ffffff"
       android:textSize="14dp"
       android:gravity="center|center_horizontal"
       android:text="登录"
       />
<ImageView
   android:id="@+id/mainNavbar_mmlIv" 
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:src="@drawable/menu_more_left"
   android:contentDescription="menu_more_left"/>
<HorizontalScrollView
   android:id="@+id/mta_hsv" 
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="8"
   android:overScrollMode="ifContentScrolls"
   android:scrollbars="none"
   android:fadingEdge="none"
   >
<LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:background="#eeead6"
    >    
   <!-- 首页 -->
   <FrameLayout
       android:id="@+id/mta_syFl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
  >         
  <Button 
      android:id="@+id/mainNavbar_mainPageBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="首页"
      android:textSize="14dp"
      android:layout_gravity="center"
      android:gravity="center|center_horizontal"
      />
  <!-- android:text="@string/mainNavbar_mainPageBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_mainPageNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 首页 -->
   <FrameLayout
       android:id="@+id/mta_sy1Fl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
  >  
  <Button 
      android:id="@+id/mainNavbar_mainPageBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="首页1"
      android:textSize="14dp"
      android:layout_gravity="center"
      android:gravity="center|center_horizontal"
      />
  <!-- android:text="@string/mainNavbar_mainPageBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_mainPageNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 首页 -->
   <FrameLayout
       android:id="@+id/mta_sy2Fl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
  >         
  <Button 
      android:id="@+id/mainNavbar_mainPageBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="首页2"
      android:textSize="14dp"
      android:layout_gravity="center"
      android:gravity="center|center_horizontal"
      />
  <!-- android:text="@string/mainNavbar_mainPageBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_mainPageNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 首页 -->
   <FrameLayout
       android:id="@+id/mta_sy3Fl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
  >  
  <Button 
      android:id="@+id/mainNavbar_mainPageBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="首页3"
      android:textSize="14dp"
      android:layout_gravity="center"
      android:gravity="center|center_horizontal"
      />
  <!-- android:text="@string/mainNavbar_mainPageBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_mainPageNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 首页 -->
   <FrameLayout
       android:id="@+id/mta_sy4Fl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
  >  
  <Button 
      android:id="@+id/mainNavbar_mainPageBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="首页4"
      android:textSize="14dp"
      android:layout_gravity="center"
      android:gravity="center|center_horizontal"
      />
  <!-- android:text="@string/mainNavbar_mainPageBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_mainPageNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 首页 -->
   <FrameLayout
       android:id="@+id/mta_sy5Fl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
  >         
  <Button 
      android:id="@+id/mainNavbar_mainPageBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="首页5"
      android:textSize="14dp"
      android:layout_gravity="center"
      android:gravity="center|center_horizontal"
      />
  <!-- android:text="@string/mainNavbar_mainPageBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_mainPageNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 我的新疆美 -->
   <FrameLayout
       android:id="@+id/mta_wdxjmFl"
       android:layout_width="60dp"
      
    android:layout_height="wrap_content"
    >         
  <Button 
      android:id="@+id/mainNavbar_myXjmeiBt"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      
      android:text="我的新疆美"
      android:textSize="14dp"
      android:gravity="bottom|center_horizontal"
      />
  <TextView 
      android:id="@+id/mainNavbar_myXjmeiNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      android:text="2"
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 我的收藏 -->
   <FrameLayout
       android:id="@+id/mta_wdscFl"
       android:layout_width="60dp"
    android:layout_height="wrap_content"
    >         
  <Button 
       android:id="@+id/mainNavbar_myCollectBt"
       android:layout_width="60dp"
       android:layout_height="wrap_content"
       android:text="我的收藏"
       android:textSize="14dp"
       android:gravity="bottom|center_horizontal"
       /> 
  <TextView 
      android:id="@+id/mainNavbar_myCollectNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      
      android:visibility="gone"
      />
   </FrameLayout>
   
   <!-- 购物车 -->
   <FrameLayout
       android:id="@+id/mta_gwcFl"
       android:layout_width="60dp"
    android:layout_height="wrap_content"
    >         
  <Button 
       android:id="@+id/mainNavbar_cartBt"
       android:layout_width="60dp"
       android:layout_height="wrap_content"
       android:text="购物车"
       android:textSize="14dp"
       android:gravity="bottom|center_horizontal"
       /> 
       <!-- android:text="@string/mainNavbar_cartBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_cartNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
      android:text="2"
      android:visibility="gone"
      />
   </FrameLayout>


   <!-- 卖家中心 -->
   <FrameLayout
       android:id="@+id/mta_mjzxFl"
       android:layout_width="60dp"
    android:layout_height="wrap_content"
   
    android:visibility="visible" >  
       
  <Button 
       android:id="@+id/mainNavbar_sellerCBt"
       android:layout_width="60dp"
       android:layout_height="wrap_content"
       android:text="卖家中心"
       android:textSize="14dp"
       android:gravity="bottom|center_horizontal"
       />
  <!-- android:text="@string/mainNavbar_randomGoodsBtTxt" -->
  <TextView 
      android:id="@+id/mainNavbar_sellerCNotifyTv"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:textSize="12dp"
      android:gravity="center"
      android:layout_gravity="top|right"
      android:textColor="#fff"
     
      android:text="2"
      android:visibility="gone"
      />   
   </FrameLayout>
    </LinearLayout>
    </HorizontalScrollView> 
    <ImageView
   android:id="@+id/mainNavbar_mmrIv" 
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:src="@drawable/menu_more_right"
   android:contentDescription="menu_more_right"/>   
</LinearLayout>
</LinearLayout>


这里需要注意的是我用一个HorizontalScrollView作为滚动菜单的面板,而且作为菜单项的FrameLayout的宽度设置成统一的固定宽度,方便实现根据菜单面板的宽度调整菜单项的宽度和显示的菜单项的个数。

2、程序调整菜单项的宽度,在activity的onWindowFocusChanged方法中获取滚动菜单面板的宽度和每个菜单项的宽度,并且调整菜单项的宽度以便占满菜单面板,代码如下:

public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
hsvWidth=mta_hsv.getWidth();
System.out.println("onWindowFocusChanged--->hsvWidth:"+hsvWidth);
btWidth=fl[0].getWidth();
System.out.println("onWindowFocusChanged--->btWidth:"+btWidth);
//System.out.println("testLl.getWidth():"+testLl.getWidth());
count=hsvWidth/(btWidth);
System.out.println("onWindowFocusChanged--->count:"+count);
int cz=hsvWidth-(btWidth)*count;
System.out.println("onWindowFocusChanged--->cz:"+cz);
int pd=(cz/count);
btWidth+=pd;
System.out.println("onWindowFocusChanged--->pd:"+pd);
for(int i=0;i<10;i++){
//fl[i].setLayoutParams(new LayoutParams(btWidth+pd, LayoutParams.WRAP_CONTENT));
LayoutParams lp=fl[i].getLayoutParams();
lp.width=btWidth;
fl[i].setLayoutParams(lp);
}

if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}

}


3、注册HorizontalScrollView的OnTouch事件监听器,代码如下:

mta_hsv.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_MOVE){

}
else if(event.getAction()==MotionEvent.ACTION_DOWN){

}
else if(event.getAction()==MotionEvent.ACTION_UP){
scrollX(v);
final View fv=v;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
scrollX(fv);
}
}, 500);//500毫秒后再次滚动一次,解决由于惯性滑动后不能滑动到合适位置的问题
}

return false;
}
private void scrollX(View v){//滑动到合适位置
int endSx=v.getScrollX();
System.out.println("endSx="+endSx);
if(endSx<=(btWidth/2)){//如果滑动距离小于半个菜单项的宽度,则滑动距离0
v.scrollTo(0, 0);
}
else if(endSx<=btWidth*1+(btWidth/2)){//如果滑动距离大于半个菜单项的宽度且小于一个半菜单项宽度,则滑动距离一个菜单项宽度
v.scrollTo(btWidth*1, 0);
}
else if(endSx<=btWidth*2+(btWidth/2)){//如果滑动距离大于一个半菜单项的宽度且小于两个半菜单项宽度,则滑动距离两个菜单项宽度
v.scrollTo(btWidth*2, 0);
}
else if(endSx<=btWidth*3+(btWidth/2)){//后面依次类推
v.scrollTo(btWidth*3, 0);
}
else if(endSx<=btWidth*4+(btWidth/2)){
v.scrollTo(btWidth*4, 0);
}
else if(endSx<=btWidth*5+(btWidth/2)){
v.scrollTo(btWidth*5, 0);
}
else if(endSx<=btWidth*6+(btWidth/2)){
v.scrollTo(btWidth*6, 0);
}
else if(endSx<=btWidth*7+(btWidth/2)){
v.scrollTo(btWidth*7, 0);
}
else if(endSx<=btWidth*8+(btWidth/2)){
v.scrollTo(btWidth*8, 0);
}
else if(endSx<=btWidth*9+(btWidth/2)){
v.scrollTo(btWidth*9, 0);
}
else if(endSx<=btWidth*10+(btWidth/2)){
v.scrollTo(btWidth*10, 0);
}

System.out.println("btWidth:"+btWidth+",count:"+count);
System.out.println("mta_hsv.getScrollX():"+mta_hsv.getScrollX());

if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}
}
});

这里通过监听松开按压事件来调整滚动位置。


Activity的完整代码如下:

package luoye.test.styletest;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class MenuTestActivity extends Activity {







int hsvWidth=0;
HorizontalScrollView mta_hsv=null;
LinearLayout testLl=null;

FrameLayout []fl=new FrameLayout[10];
int btWidth=0;
int count=0;

ImageView mainNavbar_mmlIv=null;
ImageView mainNavbar_mmrIv=null;

@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
hsvWidth=mta_hsv.getWidth();
System.out.println("onWindowFocusChanged--->hsvWidth:"+hsvWidth);
btWidth=fl[0].getWidth();
System.out.println("onWindowFocusChanged--->btWidth:"+btWidth);
//System.out.println("testLl.getWidth():"+testLl.getWidth());
count=hsvWidth/(btWidth);
System.out.println("onWindowFocusChanged--->count:"+count);
int cz=hsvWidth-(btWidth)*count;
System.out.println("onWindowFocusChanged--->cz:"+cz);
int pd=(cz/count);
btWidth+=pd;
System.out.println("onWindowFocusChanged--->pd:"+pd);
for(int i=0;i<10;i++){
//fl[i].setLayoutParams(new LayoutParams(btWidth+pd, LayoutParams.WRAP_CONTENT));
LayoutParams lp=fl[i].getLayoutParams();
lp.width=btWidth;
fl[i].setLayoutParams(lp);
}

if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.menu_test);

//取得显示边上有隐藏菜单的图片控件
mainNavbar_mmlIv=(ImageView) findViewById(R.id.mainNavbar_mmlIv);
mainNavbar_mmrIv=(ImageView) findViewById(R.id.mainNavbar_mmrIv);

//取得菜单项
fl[0]=(FrameLayout) findViewById(R.id.mta_syFl);
fl[1]=(FrameLayout) findViewById(R.id.mta_sy1Fl);
fl[2]=(FrameLayout) findViewById(R.id.mta_sy2Fl);
fl[3]=(FrameLayout) findViewById(R.id.mta_sy3Fl);
fl[4]=(FrameLayout) findViewById(R.id.mta_sy4Fl);
fl[5]=(FrameLayout) findViewById(R.id.mta_sy5Fl);
fl[6]=(FrameLayout) findViewById(R.id.mta_wdxjmFl);
fl[7]=(FrameLayout) findViewById(R.id.mta_wdscFl);
fl[8]=(FrameLayout) findViewById(R.id.mta_gwcFl);
fl[9]=(FrameLayout) findViewById(R.id.mta_mjzxFl);

//取得滚动控件
mta_hsv=(HorizontalScrollView) findViewById(R.id.mta_hsv);

// fl[0].getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
//
// @Override
// public boolean onPreDraw() {
// // TODO Auto-generated method stub
// if(btWidth==0){
// System.out.println("fl[0]:"+fl[0].getMeasuredWidth());
// btWidth=fl[0].getMeasuredWidth();
//
// count=hsvWidth/(btWidth);
// System.out.println("onWindowFocusChanged--->count:"+count);
// int cz=hsvWidth-(btWidth)*count;
// System.out.println("onWindowFocusChanged--->cz:"+cz);
// int pd=(cz/count);
// btWidth+=pd;
// System.out.println("onWindowFocusChanged--->pd:"+pd);
// for(int i=0;i<10;i++){
// //fl[i].setLayoutParams(new LayoutParams(btWidth+pd, LayoutParams.WRAP_CONTENT));
// LayoutParams lp=fl[i].getLayoutParams();
// lp.width=btWidth;
// fl[i].setLayoutParams(lp);
// }
//
// if(mta_hsv.getScrollX()>0){
// System.out.println("left:"+mta_hsv.getScrollX());
// }
// else{
// System.out.println("no-left:"+mta_hsv.getScrollX());
// }
// if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
// System.out.println("right:"+mta_hsv.getScrollX());
// }
// else{
// System.out.println("no-right:"+mta_hsv.getScrollX());
// }
//
// }
// return true;
// }
// });
//
// mta_hsv.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
//
// @Override
// public boolean onPreDraw() {
// // TODO Auto-generated method stub
// if(hsvWidth==0){
// System.out.println("mta_hsv:"+mta_hsv.getMeasuredWidth());
// hsvWidth=mta_hsv.getMeasuredWidth();
// }
// return true;
// }
// });

mta_hsv.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_MOVE){

}
else if(event.getAction()==MotionEvent.ACTION_DOWN){

}
else if(event.getAction()==MotionEvent.ACTION_UP){
scrollX(v);
final View fv=v;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
scrollX(fv);
}
}, 500);//500毫秒后再次滚动一次,解决由于惯性滑动后不能滑动到合适位置的问题
}

return false;
}
private void scrollX(View v){//滑动到合适位置
int endSx=v.getScrollX();
System.out.println("endSx="+endSx);
if(endSx<=(btWidth/2)){//如果滑动距离小于半个菜单项的宽度,则滑动距离0
v.scrollTo(0, 0);
}
else if(endSx<=btWidth*1+(btWidth/2)){//如果滑动距离大于半个菜单项的宽度且小于一个半菜单项宽度,则滑动距离一个菜单项宽度
v.scrollTo(btWidth*1, 0);
}
else if(endSx<=btWidth*2+(btWidth/2)){//如果滑动距离大于一个半菜单项的宽度且小于两个半菜单项宽度,则滑动距离两个菜单项宽度
v.scrollTo(btWidth*2, 0);
}
else if(endSx<=btWidth*3+(btWidth/2)){//后面依次类推
v.scrollTo(btWidth*3, 0);
}
else if(endSx<=btWidth*4+(btWidth/2)){
v.scrollTo(btWidth*4, 0);
}
else if(endSx<=btWidth*5+(btWidth/2)){
v.scrollTo(btWidth*5, 0);
}
else if(endSx<=btWidth*6+(btWidth/2)){
v.scrollTo(btWidth*6, 0);
}
else if(endSx<=btWidth*7+(btWidth/2)){
v.scrollTo(btWidth*7, 0);
}
else if(endSx<=btWidth*8+(btWidth/2)){
v.scrollTo(btWidth*8, 0);
}
else if(endSx<=btWidth*9+(btWidth/2)){
v.scrollTo(btWidth*9, 0);
}
else if(endSx<=btWidth*10+(btWidth/2)){
v.scrollTo(btWidth*10, 0);
}

System.out.println("btWidth:"+btWidth+",count:"+count);
System.out.println("mta_hsv.getScrollX():"+mta_hsv.getScrollX());

if(mta_hsv.getScrollX()>0){
System.out.println("left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-left:"+mta_hsv.getScrollX());
mainNavbar_mmlIv.setVisibility(View.INVISIBLE);
}
if(mta_hsv.getScrollX()<=btWidth*(10-count-1)){
System.out.println("right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.VISIBLE);
}
else{
System.out.println("no-right:"+mta_hsv.getScrollX());
mainNavbar_mmrIv.setVisibility(View.INVISIBLE);
}
}
});
}

}



完整源码下载地址:http://download.csdn.net/detail/luoye007001/5551659

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Studio中的Fragment底部导航栏是一种常见的UI设计模式,它可以让用户快速切换不同的页面或功能。在实现底部导航栏时,可以使用Android官方提供的BottomNavigationView控件,该控件可以方便地创建底部导航栏,并且支持多种样式和自定义选项。在使用BottomNavigationView时,需要创建多个Fragment,并在底部导航栏中添加对应的菜单项,然后在点击菜单项时切换到对应的Fragment。此外,还可以使用ViewPager和FragmentPagerAdapter来实现底部导航栏的滑动切换效果。总之,Android Studio提供了丰富的工具和组件来帮助开发者实现各种UI设计,包括底部导航栏。 ### 回答2: Android Studio中的Fragment底部导航栏是一种常用的界面控件,它为用户提供了方便的导航和操作方式。Fragment底部导航栏通常放置在屏幕最底部,可以显示多个图标,每个图标表示一个不同的功能或页面。当用户点击某个图标时,应用程序将自动显示相应的Fragment,实现了快速切换不同页面的功能。 开发者可以通过使用Android Studio提供的各种控件和API,来创建并配置Fragment底部导航栏。其中最常见的是使用BottomNavigationView控件,它可以在布局文件中直接添加,然后在Java代码中设置监听器以响应用户点击事件,将不同的Fragment进行切换。 除了使用BottomNavigationView外,还可以通过自定义控件,实现更丰富、更灵活的底部导航栏。例如,可以使用Material Design组件,来创建具有动画效果、菜单弹出式选项、图标和文字等自定义特性的导航栏。此外,为了确保底部导航栏的完整性,可以考虑使用CoordinatorLayout和AppBarLayout组件,以便在不同情况下自动隐藏或显示导航栏。 在使用Android Studio Fragment底部导航栏时,需要注意一些细节。例如,在替换Fragment时,应该使用add和remove方法,或者使用replace方法,以确保UI界面的平稳过渡。另外,为避免低效率的页面切换和导航栏响应,建议使用AsyncTask或其他线程技术,以提高应用的性能和流畅度。 总之,Android Studio Fragment底部导航栏是一种非常有用的界面控件,能够极大地提升应用程序的用户体验和操作效率。通过深入了解其应用方法和技术要点,可以更好地发挥其潜力,满足用户需求,并提高应用的竞争力。 ### 回答3: Android Studio的fragment底部导航栏是一个非常常用的功能。它使得应用程序用户可以更加方便地切换应用程序的不同页面。在本文中,我们将介绍如何使用Android Studio创建底部导航栏。 要在Android Studio中创建底部导航栏,您需要先创建一个新项目或打开现有项目。然后,在Project视图中,右键单击app文件夹并选择New->Activity->Bottom Navigation Activity。在弹出的新窗口中,您可以选择要在Android Studio中创建的底部导航栏的各个页面的数量。 创建该项目后,您可以将自己的代码添加到不同的片段中,以便在底部导航栏中轻松导航。此外,您可以自定义底部导航栏来控制其中的按钮和外观,例如将标签更改为图标,将颜色更改为与应用程序配色等。 在这个过程中,您可以使用Android Studio提供的多种功能来增强您的底部导航栏。例如,您可以使用代码自动完成来更快地输入代码,或者使用Android Studio的布局编辑器来更轻松地在布局中添加和删除元素。 在您创建底部导航栏后,您可以使用Android Studio的AVD管理器在模拟器中测试它,并确保它正常工作。一旦您满意结果,您就可以将应用程序打包并发布到Google Play商店或其他应用程序市场中。 总的来说,在Android Studio中创建底部导航栏是非常容易的。通过使用Android Studio提供的功能和工具,您可以创建一个整洁、功能强大的应用程序,这会让您的用户更加喜欢。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值