扩张LinearLayout以及从xml中加载menu,selector的使用
扩展LinearLayout以及从xml中加载menu,selector的使用
在包.com.test1.android.anim中 为什么单独写这个包名要用到
public class SlidingPanel extends LinearLayout {
private int speed=300;
private boolean isOpen=false;
public SlidingPanel(final Context ctxt, AttributeSet attrs) {
super(ctxt, attrs);
TypedArray a=ctxt.obtainStyledAttributes(attrs,R.styleable.SlidingPanel,0, 0);
speed=a.getInt(R.styleable.SlidingPanel_speed, 300);
a.recycle();
}
public void toggle() {
TranslateAnimation anim=null;
isOpen=!isOpen;
if (isOpen) {
setVisibility(View.VISIBLE);
anim=new TranslateAnimation(0.0f, 0.0f,getLayoutParams().height,0.0f);
}
else {
anim=new TranslateAnimation(0.0f, 0.0f, 0.0f,getLayoutParams().height);
anim.setAnimationListener(collapseListener);
}
anim.setDuration(speed);
anim.setInterpolator(new AccelerateInterpolator(1.0f));
startAnimation(anim);
}
Animation.AnimationListener collapseListener=new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {
// not needed
}
public void onAnimationStart(Animation animation) {
// not needed
}
};
}
上面扩展了一个linearLayout,正如你看见的 家红线的部分说明 我要建立一个自己的xml属性,这个xml在values/attrs/下
<resources> <declare-styleable name="SlidingPanel"> <attr name="speed" format="integer" /> <attr name="targetHeight" format="dimension" /> </declare-styleable> </resources>
在这个属性中 我建立一个速度 用来指示动画的快慢,制定一个高度 用来显示空间的高。
然后就是红色部分的调用,一定要注意引用的方法。
这里没有进行if判断 来指定这是一个必须的属性,所以默认是可以缺省的属性。
然后我就可以把扩展linearLayout加入到main中了
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.commonsware.android.anim" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.commonsware.android.anim.SlidingPanel android:id="@+id/panel" android:layout_width="fill_parent" android:layout_height="75px" android:orientation="horizontal" app:speed="250" app:targetHeight="75px" android:visibility="gone" android:background="#22FFFFFF" android:layout_alignParentBottom="true" > <Button android:layout_width="0px" android:layout_weight="1" android:layout_height="75px" android:text="Button #1" /> <Button android:layout_width="0px" android:layout_weight="1" android:layout_height="75px" android:text="Button #2" /> <Button android:layout_width="0px" android:layout_weight="1" android:layout_height="75px" android:text="Button #3" /> </com.commonsware.android.anim.SlidingPanel> </RelativeLayout>
红色部分就是你引用的时候要注意的 一定要加包名。黄色的部分是试用了绝对数值,所以你app:targetHeight="75px"修改这句话时 可能不会出现效果。
主程序就很简单了:
public class SlidingPanelDemo extends Activity { SlidingPanel panel=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); panel=(SlidingPanel)findViewById(R.id.panel); } @Override public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(getApplication()).inflate(R.menu.option, menu); return(super.onCreateOptionsMenu(menu)); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId()==R.id.toggle) { panel.toggle(); return(true); } return(super.onOptionsItemSelected(item)); } }
res/menu/option
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/toggle" android:title="Toggle Panel" android:icon="@drawable/ic_menu_preferences" /> </menu>
这里使用了从xml中加入代码。
当然整个程序还是用了动画效果,尤其是一个动画监听器。
附件中我把上面的两个button换成了checkBox和一个可以换颜色的button
为了比较加载menu不同情况 下面是从代码中加载menu
private static final int MENU_SEARCH = Menu.FIRST; private static final int MENU_PREFERENCES = Menu.FIRST + 1; private static final int MENU_HELP = Menu.FIRST + 2; /* Creates the menu items */ @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, "Search") .setIcon(android.R.drawable.ic_menu_search); menu.add(Menu.NONE, MENU_PREFERENCES, Menu.NONE, "Preferences") .setIcon(android.R.drawable.ic_menu_preferences); menu.add(Menu.NONE, MENU_HELP, Menu.NONE, "Help") .setIcon(android.R.drawable.ic_menu_help); return true; } /* Handles item selections */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_SEARCH: search(); return true; case MENU_PREFERENCES: preferences(); return true; case MENU_HELP: showHelp(); return true; } return super.onMenuItemSelected(featureId, item); }
1 楼
Wesley.S
2010-09-29
兄弟,有没有源码啊??