我想知道是否有人可以指导我如何完成以下 *** 作,
当用户拿着标题栏并向下拖动时,用户有一个标题栏;它向用户显示不同的视图.这是驻留在tabhost中的视图.
它类似于android的默认状态栏.
解决方法:
您应该尝试使用带有relativeLayout子级的VIEwFlipper并在标头上附加OntouchListeners来完成此 *** 作(类似AccordionVIEw的行为).它对我有用.
样品申请
main.xml //在res / layouts中
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <VIEwFlipper androID:ID="@+ID/flipper" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <TextVIEw androID:ID="@+ID/txt_states" androID:text="First panel" androID:layout_centerHorizontal="true" androID:textSize="18dp" androID:textStyle="bold" androID:textcolor="@androID:color/white" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> <button androID:ID="@+ID/btn_next" androID:text="@string/goto_second" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center_horizontal" androID:layout_centerHorizontal="true" androID:layout_alignParentBottom="true"/> <TextVIEw androID:ID="@+ID/txt_content1" androID:text="@string/loremipsum1" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_below="@ID/txt_states" androID:layout_above="@ID/btn_next" androID:layout_margintop="10dp" /> </relativeLayout> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <button androID:ID="@+ID/btn_prev" androID:text="@string/goto_first" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center_horizontal" androID:layout_centerHorizontal="true" /> <TextVIEw androID:ID="@+ID/txt_commands" androID:text="Second panel" androID:layout_centerHorizontal="true" androID:textSize="18dp" androID:textStyle="bold" androID:textcolor="@androID:color/white" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@ID/btn_prev" /> <TextVIEw androID:ID="@+ID/txt_content2" androID:text="@string/loremipsum2" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_below="@ID/txt_commands" androID:layout_margintop="10dp" /> </relativeLayout> </VIEwFlipper></relativeLayout>
androIDManifest.xml //仅限活动声明
<activity androID:name=".AccordionSample" androID:label="@string/app_name"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter></activity>
AccordionSample.java //您的主要活动
import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.VIEwFlipper;public class AccordionSample extends Activity implements VIEw.OntouchListener { private float oldtouchValue; private VIEwFlipper flipper; private button currentbutton; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); flipper = (VIEwFlipper)findVIEwByID(R.ID.flipper); findVIEwByID(R.ID.btn_prev).setontouchListener(this); findVIEwByID(R.ID.btn_next).setontouchListener(this); } private boolean onbuttontouchEvent(MotionEvent touchevent) { if (currentbutton == null) return false; switch (touchevent.getAction()) { case MotionEvent.ACTION_DOWN: { oldtouchValue = touchevent.getY(); break; } case MotionEvent.ACTION_UP: { float currentY = touchevent.getY(); final float diff = oldtouchValue - currentY; if ((diff < -100) && (currentbutton.getID() == R.ID.btn_prev)) { //Up --> Bottom flipper.setInAnimation(AccordionAnimation.inFromtopAnimation()); flipper.setoutAnimation(AccordionAnimation.outToBottomAnimation()); flipper.showNext(); } else if ((diff > 100) && (currentbutton.getID() == R.ID.btn_next)) { //Bottom --> Up flipper.setInAnimation(AccordionAnimation.inFromBottomAnimation()); flipper.setoutAnimation(AccordionAnimation.outTotopAnimation()); flipper.showPrevIoUs(); } break; } } currentbutton = null; return true; } @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { currentbutton = (button)v; final boolean result = this.onbuttontouchEvent(event); return result; }}
AccordionAnimation.java //用于向上和向下滑动
import androID.vIEw.animation.AccelerateInterpolator;import androID.vIEw.animation.Animation;import androID.vIEw.animation.TranslateAnimation;public class AccordionAnimation{ public static Animation inFromBottomAnimation() { Animation inFromBottom = new TranslateAnimation(Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, +1.0f, Animation.relative_TO_PARENT, 0.0f); inFromBottom.setDuration(350); inFromBottom.setInterpolator(new AccelerateInterpolator()); return inFromBottom; } public static Animation outTotopAnimation() { Animation outtotop = new TranslateAnimation(Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, -1.0f); outtotop.setDuration(350); outtotop.setInterpolator(new AccelerateInterpolator()); return outtotop; } public static Animation inFromtopAnimation() { Animation inFromtop = new TranslateAnimation(Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, -1.0f, Animation.relative_TO_PARENT, 0.0f); inFromtop.setDuration(350); inFromtop.setInterpolator(new AccelerateInterpolator()); return inFromtop; } public static Animation outToBottomAnimation() { Animation outtoBottom = new TranslateAnimation(Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, 0.0f, Animation.relative_TO_PARENT, +1.0f); outtoBottom.setDuration(350); outtoBottom.setInterpolator(new AccelerateInterpolator()); return outtoBottom; }}
strings.xml // res / values内部
<?xml version="1.0" enCoding="utf-8"?><resources> <string name="goto_first">SlIDe down to see the first panel</string> <string name="goto_second">SlIDe up to see the second panel</string> <string name="loremipsum1">Lorem ipsum dolor sit amet...</string> <string name="loremipsum2">Sed ut perspiciatis unde omnis iste...</string> <string name="app_name">AccordionSample</string></resources>
编辑
如果您想将不同的视图拆分为不同的布局xmls,请说
> first_vIEw.xml和
>第二个vIEw.xml,
你应该修改你的onCreate方法:
flipper = (VIEwFlipper)findVIEwByID(R.ID.flipper);LayoutInflater inflater = getLayoutInflater();final VIEw firstVIEw = inflater.inflate(R.layout.first_vIEw, flipper, false);flipper.addVIEw(firstVIEw);final VIEw secondVIEw = inflater.inflate(R.layout.second_vIEw, flipper, false);flipper.addVIEw(secondVIEw);firstVIEw.findVIEwByID(R.ID.btn_next).setontouchListener(this);secondVIEw.findVIEwByID(R.ID.btn_prev).setontouchListener(this);
编辑
或者更优雅高效,只需将您为不同视图创建的布局xmls包含到main.xml中:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <VIEwFlipper androID:ID="@+ID/flipper" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <include androID:ID="@+ID/flipPing_vIEw_1" layout="@layout/flipPing_vIEw_1" /> <include androID:ID="@+ID/flipPing_vIEw_2" layout="@layout/flipPing_vIEw_2" /> </VIEwFlipper></relativeLayout>
哪里
> layout / flipPing_vIEw_1.xml
包含第一个relativeLayout,
和
> layout / flipPing_vIEw_2.xml
包含第二个relativeLayout
在原始main.xml中的VIEwFlipper.
要了解有关AndroID布局的可重用性以及整体布局技术的更多信息,您应该查看Romain Guy’s great post covering layout tricks.
总结以上是内存溢出为你收集整理的android – 下拉菜单或幻灯片上的视图全部内容,希望文章能够帮你解决android – 下拉菜单或幻灯片上的视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)