看到很多软件在底部有一排按钮,于是便研究这个是怎么实现的,在android中并没有现成的控件可用,搜索网上有人说用LinearLayout的ButtonBar样式可以实现,下面便来说明下用法和实现。

不过这里说一下,在Api文档里我是没有找到说明这个ButtonBar的地方,如果有知道的还请指点一下。

首先在XML中定义这个LinearLayout,注意这句style="@android:style/ButtonBar",将这个LinearLayout定义为ButtonBar的样式

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dip" android:layout_weight="1"> </ViewFlipper> <LinearLayout style="@android:style/ButtonBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="0dip" android:paddingLeft="0dip" androidrientation="horizontal"> <ImageButton android:id="@+id/button_library_1" android:src="@drawable/button1" android:padding="0dip" android:layout_margin="0dip" android:layout_marginLeft="0dip" android:layout_marginRight="0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageButton android:id="@+id/button_picture_1" android:src="@drawable/button2" android:padding="0dip" android:layout_margin="0dip" android:layout_marginLeft="0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>

这里加了一个viewFlipper,当按钮按下去时使用它来做界面切换,在内层的LinearLayout中我使用了两个ImageButton,也可以使用Button,随意定义即可。

代码中只需要将两个View添加到ViewFlipper中,然后将两个ImageButton的OnClickListener分别定义为显示刚刚添加的两个View即可。如下:

public class TabTest extends Activity { ViewFlipper vf; ImageButton b1,b2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabtest); LayoutInflater li = this.getLayoutInflater(); View view = li.inflate(R.layout.flip1, null); TextView tv = new TextView(this); tv.setText("test text"); vf = (ViewFlipper) findViewById(R.id.flipper); vf.addView(view,0); vf.addView(tv,1); b1 = (ImageButton)findViewById(R.id.button_library_1); b2 = (ImageButton)findViewById(R.id.button_picture_1); b1.setBackgroundDrawable(null); b1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { vf.setDisplayedChild(0); } }); b2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { vf.setDisplayedChild(1); } }); } }

这只是一个很简陋的实现,还需要很多美化工作,这里不详述了


更多相关文章

  1. 《Android(安卓)Dev Guide》系列教程16:用户通知(3)创建状态栏通知
  2. Android之自定义控件
  3. Android(安卓)5.0+ 自定义普通按钮的ripple波纹效果
  4. 【Android】Android自动开关机实现
  5. Android学习笔记---自定义View#01
  6. 自定义View关于measure流程的基本思路整理
  7. 自定义View框架完全解析
  8. Android(安卓)自定义属性研究
  9. Android中ContentProvider的实现及定义自己的ContentProvider

随机推荐

  1. android protobuf错误
  2. Android中如何获得本机号码信息
  3. Android(安卓)加载动画逐帧动画透明弹窗
  4. Android(安卓)Adapter
  5. [Android]ListView中分割线的设置
  6. Android中文SDK-----Android新手入门
  7. android 自定义进度条
  8. 解析Android消息处理机制 ——Handler/Th
  9. Android(安卓)获取界面上的元素
  10. Android(安卓)ImageView图片自适应