- 浏览: 238818 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
maotou1988:
android基于netty的发送图片与文字demo,楼主共勉 ...
在android中使用netty框架通信 -
ZSRTFAT:
学习了,谢谢分享
android 基于surfaceview 的多点触控实现 -
charlotte:
学习了!!!!!
Java nio 客户端连接Server -
nilo:
您好, 本人菜鸟, 正在学习设计模式. 当我学习完简单工厂模式 ...
策略模式 -
fokman:
一江春水邀明月 写道缺少SocketInfoUtils Th ...
Java nio 客户端连接Server
在编写android应用程序的过程中,经常会使用到导航控件。如果每一个activity都需要用到导航控件,可以将这个导航控件独立出来,方便程序开发。
写一个导航的代码类:
public class NavigationBar extends RelativeLayout implements OnClickListener { public static final int NAVIGATION_BUTTON_LEFT = 0; public static final int NAVIGATION_BUTTON_RIGHT = 1; private Context mContext; private NavigationBarListener mListener; public NavigationBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public NavigationBar(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public NavigationBar(Context context) { super(context); init(context); } private void init(Context context) { mContext = context; RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1, -2); this.setLayoutParams(lp); this.setBackgroundResource(R.drawable.navigation_bar_bg); } public void setLeftBarButton(String title) { setButton(title, NAVIGATION_BUTTON_LEFT); } public void setRightBarButton(String title) { setButton(title, NAVIGATION_BUTTON_RIGHT); } private void setButton(String title, int which) { // remove the old button (if there is one) Button oldButton = (Button) this.findViewWithTag(new Integer(which)); if (oldButton != null) this.removeView(oldButton); Button newButton = new Button(mContext); newButton.setTag(new Integer(which)); // used to determine which button is pressed and to remove old buttons // set OnClickListener newButton.setOnClickListener(this); // set LayoutParams RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2); if (which == NAVIGATION_BUTTON_LEFT) lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); else if (which == NAVIGATION_BUTTON_RIGHT) lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); else throw new IllegalArgumentException("Parameter 'which' must be 0 or 1"); lp.addRule(RelativeLayout.CENTER_VERTICAL); lp.setMargins(10, 0, 10, 0); newButton.setLayoutParams(lp); // set button text newButton.setText(title); newButton.setTextSize(12); newButton.setTextColor(Color.WHITE); // set button drawable newButton.setBackgroundResource(R.drawable.navigation_bar_btn); // add button this.addView(newButton); } public void setBarTitle(String title) { // remove old title (if exists) TextView oldTitle = (TextView) this.findViewWithTag("title"); if (oldTitle != null) this.removeView(oldTitle); TextView newTitle = new TextView(mContext); newTitle.setTag("title"); // set LayoutParams RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2); lp.addRule(RelativeLayout.CENTER_IN_PARENT); lp.setMargins(0, 30, 0, 30); newTitle.setLayoutParams(lp); // set text newTitle.setText(title); newTitle.setTextSize(22); newTitle.setTextColor(Color.WHITE); // add title to NavigationBar this.addView(newTitle); } public void setNavigationBarListener(NavigationBarListener listener) { mListener = listener; } @Override public void onClick(View v) { int which = ((Integer) v.getTag()).intValue(); if (mListener != null) { mListener.OnNavigationButtonClick(which); } } /** * Listener for NavigationBar. */ public interface NavigationBarListener { /** * Called when the user presses either of the buttons on the NavigationBar. * * @param which - indicates which button was pressed, ie: NavigationBar.NAVIGATION_BUTTON_LEFT */ public void OnNavigationButtonClick(int which); } }
背景样式定义navigation_bar_bg.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:angle="270" android:endColor="#ff050708" android:startColor="#ff595959" android:type="linear" /> </shape> </item> </selector>
导航按钮样式定义navigation_bar_btn.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <gradient android:angle="270" android:endColor="#ff495a95" android:startColor="#ff7684c1" /> <stroke android:width="1px" android:color="#FF000000" /> <corners android:radius="6dp" /> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape> </item> <item> <shape> <gradient android:angle="270" android:endColor="#ff999999" android:startColor="#ff333333" /> <stroke android:width="1px" android:color="#FF000000" /> <corners android:radius="6dp" /> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape> </item> </selector>
那么在activity怎么使用呢?
activity调用代码如下:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.change_device); NavigationBar nb = (NavigationBar)findViewById(R.id.detailNavBar); nb.setLeftBarButton(getString(R.string.cancel)); nb.setRightBarButton(getString(R.string.save)); nb.setBarTitle(getString(R.string.details)); NavigationBar.NavigationBarListener nbl = new NavigationBar.NavigationBarListener() { @Override public void OnNavigationButtonClick(int which) { if (which == NavigationBar.NAVIGATION_BUTTON_LEFT) { finish(); } else { } } }; nb.setNavigationBarListener(nbl); }
在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:background="@android:color/white"
android:orientation="vertical" >
<com.NavigationBar
android:id="@+id/detailNavBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/deviceName"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="@+id/removeBtn"
android:layout_width="260dp"
android:background="@drawable/btn_red"
android:layout_height="wrap_content"
style="@style/ButtonText"
android:layout_gravity="center_horizontal"
android:text="@string/remove" />
</LinearLayout>
现在看看效果图:
那么在你以后想使用这个控件的时候,就可以直接调用了。
发表评论
-
获得手机相关信息的实现方法
2015-04-04 00:55 774本文介绍了如何通过android提供的servi ... -
android判断当前sdk版本以及是否是平板
2013-11-19 13:53 1278最近做了一个项目中需要使用appWidget,但是 ... -
android 程序检查当前应用是否在运行
2012-11-09 17:05 1065最近做一个项目,需要知道当前的android 程序是 ... -
netty 找不到org.jboss.netty.bootstrap.ClientBootstrap解决
2012-09-25 14:20 4017今天碰到一个莫名其妙的问题,在做socket连接的时候发 ... -
Netty 之Oio程序编写
2012-09-24 14:06 4504今日将android里面使用的socket 方式改成 ... -
定义文本样式
2012-09-06 14:19 1061android程序中的按钮经常需要定义文字的大小,是否加粗,居 ... -
点击按钮显示不同的状态
2012-09-06 14:05 978android给我们提供的UI还比较丰富,但是基本上都 ... -
android 两个listView 共存的显示问题
2012-08-07 09:55 2486如果在一个layout里面需要定义有两个listvie ... -
定义带箭头的Button 样式
2012-08-02 10:58 11152定义一个圆角的Button 样式 <style ... -
android UI入门三 自定义背景样式
2012-08-01 15:23 1369在android 开发应用中,大部份的开发人员 ... -
android UI入门二 定义图片横竖屏切换正确显示
2012-07-30 13:20 3205android开发的时候通常会使用到横竖屏切换,但是 ... -
android UI入门一
2012-07-24 12:20 913在ios系统中,常常看到绚丽的画面让人心动不已。然而在 ... -
android spinner 实现国码选择
2012-07-13 15:33 3925android 中得Spinner相当于是一个下拉框 ... -
在自己的android应用中调用打电话实现
2012-07-13 15:10 1049最近在做一个项目,需要在自己写的应用中调用打电话给别人 ... -
android 定义圆角Button
2012-06-30 15:31 5028定义styles.xml <?xml version= ... -
android observer 处理handler消息
2012-06-12 09:41 2048在开发android网络应用程序,通常都会使用han ... -
android wifi 处理
2012-06-12 09:35 3183在android应用程序中可以很方便的使用wifi来处理不同的 ... -
android canvas 使用
2012-04-06 09:38 7629Canvas 又称为画布,在游戏开发过程中是一个经常使用的类。 ... -
android 基于surfaceview 的多点触控实现
2012-04-06 08:55 12368hello,各位朋友。最近接到一个功能需要实现多 ... -
给android imageView(图片) 添加超链接
2012-03-29 09:29 8048hi ,各位好朋友,最近忙于开发没有时间更新博客。 有很多朋 ...
相关推荐
Android自定义导航控件动态添加控件一
希望大家喜欢一起学习。 本文目录主要如下: 1.自定义控件属性的定义 2.自定义控件的java代码 3.自定义控件属性的用法 4.控件项目的打包处理 5.其他项目的使用
Andrid自定义控件实现导航条,实现了滚动和快速滚动的功能,还实现了当未完全显示在屏幕上的选项被选中时会自动滚动至完全显示在屏幕上的功能。具体讲解看我的博客,1分只为对得起自己的劳动成果
android 自定义组合控件 顶部导航栏
前段时间android项目中用到一个可以滑动的数量可变的导航栏,在网上也没找到合适的替代品,所以就自定义了个该控件,在此分享一下该控件,给有需要的人。
自定义导航控件 动态添加控件,当导航条目超出屏幕时可通过代码动态生成控件添加到布局的方式导航栏目
android自定义控件ImageTextButton导航按钮,androidstudio项目,可以直接运行,希望能给大家学习上,工作上都来来帮助
android 自定义中间带圆形按钮的底部导航控件,使用方便
android自定义控件-侧滑菜单.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
代码中有详细的注释,功能比较简单,主要是学习使用,有积分的可以下载支持一下,如果实在没有积分可以查找我的《Android 自定义控件详解》这篇文章学习。
自定义了顶部导航栏,在开发过程中可直接引用自定义的控件,省去了为每个Activity配置顶部导航栏的时间和精力。
通过组合的方式自定义控件,组合方式。当前控件类从容器类继承,并将若干个控件添加到当前的容器中。
android 自定义中间带圆形按钮的底部导航控件.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
左侧竖向选项卡TAB导航烂,支持viewpager,不依赖viewpager,支持自定义tab样式,自带切换动画
主要介绍了Android自定义顶部导航栏控件实例代码,需要的朋友可以参考下
android控件研究,自定义滑动按钮,有注释
CustomView-Android自定义控件 此项目包含一系列自己在开发过程中的自定义控件. 包含两部分: 工具类在com.asia.custom.util包下 自定义控件在com.asia.custom.view包下 ##工具包 Dlog 此类为Android的Log包装类.与...
一个自定义控件继承自ScrollView,下拉时header会放大松开后会恢复原状,上滑时header会被下面的内容吃掉盖住而且会稍稍往上滑,在header高度范围内滑动时导航栏背景和导航栏的按钮会反向改变透明度形成一种对比效果。
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...