如何在一个Activity中通过不同的VIEW来刷新界面? 如何在一个Activity中通过不同的VIEW来刷新界面?目的是在一个Activity里面,底部设置三个按钮,通过这个三个按钮,设置不同的VIEW,来改变界面。那位朋友能否给个DEMO,刚学android不久,被这个问题一直困扰着。能否给个完整的的demo代码。 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在布局文件中定义布局,在Activity中通过FindViewByID找到布局,给按钮绑定事件来改变布局背景色,布局方向等等 在布局文件中定义布局,在Activity中通过FindViewByID找到布局,给按钮绑定事件来改变布局背景色,布局方向等。LayoutInflater的使用,LayoutInflater的作用类似于findViewById()。 不同点是LayoutInflater是用来寻找layout下xml布局文件,并且实例化! findViewById()是找具体xml下的具体widget控件(如Button,TextView等)。自定义的切换卡不知道行不行?不行,我想要的效果是类似于TabHost里面,界面切换。 就是点一下,切换一个页面?用ViewFlipper 楼上这位同学,有没有关于ViewFlipper 的简单的demo。 <?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/details" android:layout_width="fill_parent" android:layout_height="fill_parent" android:persistentDrawingCache="animation" android:flipInterval="1000" android:inAnimation="@anim/push_left_in" android:outAnimation="@anim/push_left_out" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Next" android:id="@+id/Button_next1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/image1" android:src="@drawable/exit_bt" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ImageView> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Next" android:id="@+id/Button_next2" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/image2" android:src="@drawable/map_bt" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ImageView> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Next" android:id="@+id/Button_next3" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/image3" android:src="@drawable/min_bt" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ImageView> </LinearLayout> </ViewFlipper> </LinearLayout>package test.main;import test.tool.ViewAnimation;import android.app.Activity;import android.os.Bundle;import android.view.GestureDetector;import android.view.GestureDetector.OnDoubleTapListener;import android.view.GestureDetector.OnGestureListener;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import android.widget.ViewFlipper;public class FlipperDemo extends Activity implements OnGestureListener , OnDoubleTapListener { private ViewFlipper mViewFlipper; private GestureDetector mGestureDetector; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.flipper_demo); mGestureDetector = new GestureDetector(this);// mGestureDetector Button buttonNext1 = (Button) findViewById(R.id.Button_next1); mViewFlipper = (ViewFlipper) findViewById(R.id.details); buttonNext1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { //在layout中定义的属性,也可以在代码中指定 // mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); // mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); // mViewFlipper.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES); // mViewFlipper.setFlipInterval(1000); mViewFlipper.showNext(); //调用下面的函数将会循环显示mViewFlipper内的所有View。 // mViewFlipper.startFlipping(); } }); Button buttonNext2 = (Button) findViewById(R.id.Button_next2); buttonNext2.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { mViewFlipper.showNext(); } }); Button buttonNext3 = (Button) findViewById(R.id.Button_next3); buttonNext3.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { mViewFlipper.showNext(); } }); } @Override public boolean onDoubleTap(MotionEvent e) { if(mViewFlipper.isFlipping()) { mViewFlipper.stopFlipping(); }else { mViewFlipper.startFlipping(); } return true; } @Override public boolean onDoubleTapEvent(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e1.getX() > e2.getX()) {//move to left mViewFlipper.showNext(); }else if(e1.getX() < e2.getX()) { mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in); mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out); mViewFlipper.showPrevious(); mViewFlipper.setFlipInterval(1000); mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); }else { return false; } return true; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onTouchEvent(MotionEvent event) { // TODOAuto-generated method stub return this.mGestureDetector.onTouchEvent(event); }} 上面是一个画面很简单的DEMO,很久前我参照网上的说明给自己写的练习。手边没测试的环境执行,应该是可以正确运行的,好像还带滑屏切换的 那为什么不用tabHost呢? 也可以加滑屏的效果的啊 tabhost不是用view来处理,现在我想学习一下,一个屏幕通过View来刷新的方法。 我查了一些资料,参考了一些demo。发现他们是通过 view 和ViewGroup来实现这种效果。那位同学有没有实现过这种效果,能否给一个demo来学习一下。 楼主 你对 android的理解 还 很浅 多看看 android的知识点 结合 sdk里面的 api比 什么都好 。sdk/docs/index.html -->reference (D:\Android\docs\reference\android\widget\TabHost.html)里面查看 tabhost 里面有个 连接 例子的 Tab Layout tutorial. 对android我的理解确实还很浅,tabhost只能停泊在界面的顶部或底部。而我想要的效果是在界面中任何地方都可以实现,我目前找到的最好解决办法是动态添加view,通过代码来实现。 获取你要改变的地方的layout , 实例化改变后的xml addView进去就行了 这个很简单,可以事前写三个layout,然后监听三个Button按钮事件,在监听事件里设置不同的layout,就会得到不同的View final LayoutInflater inflater = LayoutInflater.from(this); Button btn = (Button) findViewById(R.id.Button01); Button btn2 = (Button) findViewById(R.id.Button02); Button btn3 = (Button) findViewById(R.id.Button03); final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LinearLayout layout = (LinearLayout) inflater.inflate( R.layout.listview, null).findViewById(R.id.layout); ListView lv=(ListView)layout.getChildAt(0); lv.setAdapter(new listAdapter(myActivity.this)); lin.removeAllViews(); lin.addView(layout); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LinearLayout layout = (LinearLayout) inflater.inflate( R.layout.hello, null).findViewById(R.id.hellolayout); TextView lv=(TextView)layout.getChildAt(0); lv.setText("test2"); lv.setTextColor(Color.BLUE); lin.removeAllViews(); lin.addView(layout); } }); btn3.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub LinearLayout layout = (LinearLayout) inflater.inflate( R.layout.otherview, null).findViewById(R.id.otherlayout); TextView otherTv = (TextView)layout.getChildAt(0); otherTv.setTextColor(Color.BLACK); Button btn1 = (Button)layout.getChildAt(1); btn1.setText("按钮a"); Button btn2 = (Button)layout.getChildAt(2); btn2.setText("按钮b"); Button btn3 = (Button)layout.getChildAt(3); btn3.setText("按钮c"); lin.removeAllViews(); lin.addView(layout); } });楼上两位同学说的没错,我就是用添加VIEW 的方式实现这种效果。最近几天在优化整理这种方式,csdn没法上传源码。 问题前几天就解决了,一直没来及时结贴。上面的代码就是我自己按照模拟别人的代码写的一个简单的测试demo。 个人开发者 机型问题 使用AsyncTask更新listView加载图片的问题 使用Ant编译zxing-1.6产生的问题 android+tomcat+sql的无线点餐系统求指导 <<android应用开发揭秘>>其中一个例子错误 大家好 请问这个效果怎么实现阿 android 绘图问题 请问如何弄一个全屏的背景图=,=问题比较白,不过求指导。。谢谢 求如何在GridView底部加一个按钮 android 如何 获得监听某一广播的所有程序 bluetoothsocket串口问题 音乐播放器代码执行没反映
LayoutInflater的使用,LayoutInflater的作用类似于findViewById()。
不同点是LayoutInflater是用来寻找layout下xml布局文件,并且实例化!
findViewById()是找具体xml下的具体widget控件(如Button,TextView等)。自定义的切换卡不知道行不行?
不行,我想要的效果是类似于TabHost里面,界面切换。
<?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/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:persistentDrawingCache="animation"
android:flipInterval="1000"
android:inAnimation="@anim/push_left_in"
android:outAnimation="@anim/push_left_out"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image1"
android:src="@drawable/exit_bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image2"
android:src="@drawable/map_bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image3"
android:src="@drawable/min_bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>package test.main;import test.tool.ViewAnimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;public class FlipperDemo extends Activity implements OnGestureListener , OnDoubleTapListener {
private ViewFlipper mViewFlipper;
private GestureDetector mGestureDetector; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper_demo);
mGestureDetector = new GestureDetector(this);
// mGestureDetector
Button buttonNext1 = (Button) findViewById(R.id.Button_next1);
mViewFlipper = (ViewFlipper) findViewById(R.id.details);
buttonNext1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//在layout中定义的属性,也可以在代码中指定
// mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
// mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
// mViewFlipper.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
// mViewFlipper.setFlipInterval(1000);
mViewFlipper.showNext();
//调用下面的函数将会循环显示mViewFlipper内的所有View。
// mViewFlipper.startFlipping();
}
});
Button buttonNext2 = (Button) findViewById(R.id.Button_next2);
buttonNext2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
Button buttonNext3 = (Button) findViewById(R.id.Button_next3);
buttonNext3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
}
@Override
public boolean onDoubleTap(MotionEvent e) {
if(mViewFlipper.isFlipping()) {
mViewFlipper.stopFlipping();
}else {
mViewFlipper.startFlipping();
}
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX() > e2.getX()) {//move to left
mViewFlipper.showNext();
}else if(e1.getX() < e2.getX()) {
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);
mViewFlipper.showPrevious();
mViewFlipper.setFlipInterval(1000);
mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
}else {
return false;
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODOAuto-generated method stub
return this.mGestureDetector.onTouchEvent(event);
}
}
view 和ViewGroup来实现这种效果。那位同学有没有实现过这种效果,能否给一个demo来学习一下。
sdk/docs/index.html -->reference
(D:\Android\docs\reference\android\widget\TabHost.html)
里面查看 tabhost 里面有个 连接 例子的
Tab Layout tutorial.
final LayoutInflater inflater = LayoutInflater.from(this);
Button btn = (Button) findViewById(R.id.Button01);
Button btn2 = (Button) findViewById(R.id.Button02);
Button btn3 = (Button) findViewById(R.id.Button03);
final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.listview, null).findViewById(R.id.layout);
ListView lv=(ListView)layout.getChildAt(0);
lv.setAdapter(new listAdapter(myActivity.this));
lin.removeAllViews();
lin.addView(layout);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.hello, null).findViewById(R.id.hellolayout);
TextView lv=(TextView)layout.getChildAt(0);
lv.setText("test2");
lv.setTextColor(Color.BLUE);
lin.removeAllViews();
lin.addView(layout);
}
});
btn3.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.otherview, null).findViewById(R.id.otherlayout);
TextView otherTv = (TextView)layout.getChildAt(0);
otherTv.setTextColor(Color.BLACK);
Button btn1 = (Button)layout.getChildAt(1);
btn1.setText("按钮a");
Button btn2 = (Button)layout.getChildAt(2);
btn2.setText("按钮b");
Button btn3 = (Button)layout.getChildAt(3);
btn3.setText("按钮c");
lin.removeAllViews();
lin.addView(layout);
}
});
楼上两位同学说的没错,我就是用添加VIEW 的方式实现这种效果。最近几天在优化整理这种方式,csdn没法上传源码。