Giter Site home page Giter Site logo

tabnavigate's Introduction

tabnavigate

Android底部导航栏封装 一、概述 现在Android底部导航的设计风格已经十分普及,实现起来都比较繁琐。本着程序猿能偷懒就偷懒的原则,所以笔者封装了下View。

目前比较流行的解决方案大致有一下几种:

1、viewpager + tabhost + fragment

2、viewpager + RadioButton + fragment

这些实现起来都比较繁琐。

二、预期效果 用比较少的代码达到底部导航的效果,

三、实现过程 详细参考 http://blog.csdn.net/linuxiao/article/details/39934987 四、用法 4.1 定义item的选择器item0�,item1,item2,item3 最主要要定义好state_selected

代码如下 [html] view plaincopy

<item android:drawable="@drawable/tab_address_pressed" android:state_pressed="true"/>  
<item android:drawable="@drawable/tab_address_pressed" android:state_selected="true"></item>  
<item android:drawable="@drawable/tab_address_pressed" android:state_focused="true" android:state_pressed="false"></item>  
<item android:drawable="@drawable/tab_address_normal" android:state_focused="false" android:state_pressed="false"/>  
<item android:drawable="@drawable/tab_address_normal" android:state_focused="false"/>  

4.2 activity_main.xml 代码如下: [html] view plaincopy

<com.linuxiao.lib.tabnavigate.TabNavigateLayout  
    android:id="@+id/tabNavigateLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:layout_alignParentBottom="true"  
    android:background="@drawable/bottom_bar" >  

    <com.linuxiao.lib.tabnavigate.TabNavigateItem  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        app:src="@drawable/item_0"  
        app:text="首页" >  
    </com.linuxiao.lib.tabnavigate.TabNavigateItem>  

    <com.linuxiao.lib.tabnavigate.TabNavigateItem  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        app:src="@drawable/item_1"  
        app:text="消息" >  
    </com.linuxiao.lib.tabnavigate.TabNavigateItem>  

    <com.linuxiao.lib.tabnavigate.TabNavigateItem  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        app:src="@drawable/item_2"  
        app:text="更多" >  
    </com.linuxiao.lib.tabnavigate.TabNavigateItem>  

    <com.linuxiao.lib.tabnavigate.TabNavigateItem  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        app:src="@drawable/item_3"  
        app:text="设置" >  
    </com.linuxiao.lib.tabnavigate.TabNavigateItem>  
</com.linuxiao.lib.tabnavigate.TabNavigateLayout>  

<FrameLayout  
    android:id="@+id/container"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_above="@+id/tabNavigateLayout1"  
    android:layout_alignLeft="@+id/tabNavigateLayout1"  
    android:layout_alignParentTop="true"  
    android:layout_alignRight="@+id/tabNavigateLayout1"   
    android:background="#cdcdcd">  

</FrameLayout>  
4.3 MainActivity 比较简单代码如下:

public class MainActivity extends FragmentActivity implements OnItemSelectedListener {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    TabNavigateLayout navigateLayout = (TabNavigateLayout)findViewById(R.id.tabNavigateLayout1);  
    navigateLayout.setOnItemSelectedListener(this);  
    getSupportFragmentManager().beginTransaction().add(R.id.container, new ItemFragment(0)).commit();  
}  

@Override  
public void OnItemSelected(View v, int position) {  
    getSupportFragmentManager().beginTransaction().replace(R.id.container, new ItemFragment(position)).commit();  
}  

}

4.4 ItemFragment

/**

*/
public class ItemFragment extends Fragment {

private int position;  

public ItemFragment(int position) {  
    this.position = position;  
}  
/* (non-Javadoc) 
 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) 
 */  
@Override  
public View onCreateView(LayoutInflater inflater, ViewGroup container,  
        Bundle savedInstanceState) {  
    TextView tv = new TextView(getActivity());  
    tv.setText("这是第" + position + "个Fragment");  
    return tv;  
}  

}

转载请注明出处http://blog.csdn.net/linuxiao/article/details/39934987

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.