【移动应用开发技术】怎么使用Android实现底部切换标签_第1页
【移动应用开发技术】怎么使用Android实现底部切换标签_第2页
【移动应用开发技术】怎么使用Android实现底部切换标签_第3页
【移动应用开发技术】怎么使用Android实现底部切换标签_第4页
【移动应用开发技术】怎么使用Android实现底部切换标签_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么使用Android实现底部切换标签

这篇文章主要介绍怎么使用Android实现底部切换标签,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下实现底部通用切换标签,嵌套Fragment,方便自定义布局自定义控件:widget_tab_view.xml<?xml

version="1.0"

encoding="utf-8"?>

<merge

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:id="@+id/tab_image"

android:layout_width="20dp"

android:layout_height="20dp"

/>

<TextView

android:id="@+id/tab_label"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#666666"

android:textSize="12sp"

/>

</merge>定义单个标签public

class

TabView

extends

LinearLayout

{

private

ImageView

mTabImage;

private

TextView

mTabLable;

public

TabView(Context

context)

{

super(context);

initView(context);

}

public

TabView(Context

context,

@Nullable

AttributeSet

attrs)

{

super(context,

attrs);

initView(context);

}

public

TabView(Context

context,

@Nullable

AttributeSet

attrs,

int

defStyleAttr)

{

super(context,

attrs,

defStyleAttr);

initView(context);

}

private

void

initView(Context

context)

{

setOrientation(VERTICAL);

setGravity(Gravity.CENTER);

LayoutInflater.from(context).inflate(R.layout.widget_tab_view,

this,

true);

mTabImage

=

(ImageView)

findViewById(R.id.tab_image);

mTabLable

=

(TextView)

findViewById(R.id.tab_label);

}

public

void

initData(TabItem

tabItem)

{

mTabImage.setImageResource(tabItem.imageResId);

mTabLable.setText(tabItem.lableResId);

}

}定义单个标签的entitypublic

class

TabItem

{

public

int

imageResId;

public

int

lableResId;

public

Class<?

extends

Fragment>

tagFragmentClz;

public

TabItem(int

imageResId,

int

lableResId)

{

this.imageResId

=

imageResId;

this.lableResId

=

lableResId;

}

public

TabItem(int

imageResId,

int

lableResId,

Class<?

extends

Fragment>

tagFragmentClz)

{

this.imageResId

=

imageResId;

this.lableResId

=

lableResId;

this.tagFragmentClz

=

tagFragmentClz;

}

}定义底部切换标签控件public

class

BottomTabLayout

extends

LinearLayout

implements

View.OnClickListener

{

private

ArrayList<TabItem>

tabs;

private

OnTabClickListener

listener;

private

int

tabCount;

private

View

selectedView;

public

BottomTabLayout(Context

context)

{

super(context);

initView();

}

public

BottomTabLayout(Context

context,

@Nullable

AttributeSet

attrs)

{

super(context,

attrs);

initView();

}

public

BottomTabLayout(Context

context,

@Nullable

AttributeSet

attrs,

int

defStyleAttr)

{

super(context,

attrs,

defStyleAttr);

initView();

}

private

void

initView()

{

setOrientation(HORIZONTAL);

}

public

void

setCurrentTab(int

i)

{

if

(i

<

tabCount

&&

i

>=

0)

{

View

view

=

getChildAt(i);

onClick(view);

}

}

public

void

initData(ArrayList<TabItem>

tabs,

OnTabClickListener

listener)

{

this.tabs

=

tabs;

this.listener

=

listener;

LayoutParams

params

=

new

LayoutParams(0,

ViewGroup.LayoutParams.MATCH_PARENT);

params.weight

=

1;

params.gravity

=

Gravity.CENTER;

if

(tabs

!=

null

&&

tabs.size()

>

0)

{

tabCount

=

tabs.size();

TabView

mTabView

=

null;

for

(int

i

=

0,

len

=

tabs.size();

i

<

len;

i++)

{

mTabView

=

new

TabView(getContext());

mTabView.setTag(tabs.get(i));

mTabView.initData(tabs.get(i));

mTabView.setOnClickListener(this);

addView(mTabView,

params);

}

}

else

{

throw

new

IllegalArgumentException("tabs

can

not

be

empty");

}

}

@Override

public

void

onClick(View

view)

{

if

(selectedView

!=

view)

{

listener.onTabClick((TabItem)

view.getTag());

view.setSelected(true);

if

(selectedView

!=

null)

{

selectedView.setSelected(false);

}

selectedView

=

view;

}

}

public

interface

OnTabClickListener

{

void

onTabClick(TabItem

tabItem);

}

}Activitypublic

class

MainActivity

extends

AppCompatActivity

implements

BottomTabLayout.OnTabClickListener

{

private

BottomTabLayout

tab_layout;

private

ArrayList<TabItem>

tabs;

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setTitle("底部切换标签");

tab_layout

=

(BottomTabLayout)

findViewById(R.id.tab_layout);

initBottomTab();

tab_layout.setCurrentTab(0);

}

private

void

initBottomTab()

{

tabs

=

new

ArrayList<>();

tabs.add(new

TabItem(R.drawable.selector_tab_msg,

R.string.wechat,

OneFragment.class));

tabs.add(new

TabItem(R.drawable.selector_tab_contact,

R.string.contacts,

TwoFragment.class));

tabs.add(new

TabItem(R.drawable.selector_tab_moments,

R.string.discover,

ThreeFragment.class));

tabs.add(new

TabItem(R.drawable.selector_tab_profile,

R.string.me,

FourFragment.class));

tab_layout.initData(tabs,

this);

}

private

Fragment

lastFragment;

@Override

public

void

onTabClick(TabItem

tabItem)

{

try

{

Fragment

tmpFragment

=

getSupportFragmentManager().findFragmentByTag(tabItem.tagFragmentClz.getSimpleName());

FragmentTransaction

transaction

=

getSupportFragmentManager().beginTransaction();

if

(tmpFragment

==

null)

{

tmpFragment

=

tabItem.tagFragmentClz.newInstance();

transaction.add(R.id.fl_container,

tmpFragment,

tabItem.tagFragmentClz.getSimpleName());

if

(lastFragment

!=

null)

{

transaction.hide(lastFragment);

}

mitAllowingStateLoss();

}

else

{

transaction.show(tmpFragment);

if

(lastFragment

!=

null)

{

transaction.hide(lastFragment);

}

mitAllowingStateLoss();

}

lastFragment

=

tmpFragment;

}

catch

(Exception

e)

{

e.printStackTrace();

}

}

}布局文件<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="ma

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论