导航:首页 > 操作系统 > androidremoveview

androidremoveview

发布时间:2023-04-09 23:48:07

android removeView用法

直接给你上代码吧,写了我半个小时,经过了我的测试了的~

运行下就能看到结果了~关键的remove的时候有给你写注释~

布局的layout文件内容:
----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/linearlayout">

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="@+id/add"
android:text="Add" android:layout_width="100px"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="100px" android:text="Remove" android:id="@+id/remove"></Button>
</LinearLayout>
<TextView android:id="@+id/TextView01" android:text="This is textView."
android:layout_width="fill_parent" android:gravity="center"
android:layout_height="50px"></TextView>

</LinearLayout>
----------------------------------------------------------------------------------

对应Activity的内容:
----------------------------------------------------------------------------------
package com.foxconn.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class DialogTest extends Activity implements OnClickListener {

private Button add_btn, remove_btn;
private LinearLayout linearLayout;
private int index = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
register();
}

private void register() {
add_btn.setOnClickListener(this);
remove_btn.setOnClickListener(this);
}

private void findViews() {
add_btn = (Button) findViewById(R.id.add);
remove_btn = (Button) findViewById(R.id.remove);
linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
}

protected View createView() {
Button btn = new Button(this);
btn.setId(index++);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setText("aaaaaa" + index);
return btn;
}

private void removeView() {
//获取linearlayout子view的个数
int count = linearLayout.getChildCount();
//研究整个LAYOUT布局,第0位的是含add和remove两个button的layout
//第count-1个是那个文字被置中的textview
//因此,在remove的时候,只能操作的是0<location<count-1这个范围的
//在执行每次remove时,我们从count-2的位置即textview上面的那个控件开始删除~
if (count - 2 > 0) {
//count-2>0用来判断当前linearlayout子view数多于2个,即还有我们点add增加的button
linearLayout.removeViewAt(count - 2);
}
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
linearLayout.addView(createView(), 1);
break;
case R.id.remove:
removeView();
break;
default:
break;
}
}
}
----------------------------------------------------------------------------------

⑵ android 系统级的悬浮窗实现

当我们在使用的app的时候,如果需要实时观测到某个功能的实时进度并且不影响其他的操作的时候或者不影响使用其他应用的时候,系统级的悬浮球是个非常不错的选择。

public class QueueUpFloatService extends Service {

/**

* 启动服务并传值

*

* @param activity 启动服务的activity

* @param modeBean 数据对象

*/

public static void launchService(Activity activity, ModeBean modeBean) {

try {

        Intent intent =new Intent(activity, QueueUpFloatService.class);

        Bundle bundle =new Bundle();

        bundle.putSerializable(KEY_MODEL, modeBean);

        intent.putExtras(bundle);

        activity.startService(intent);

    }catch (Exception e) {

        e.printStackTrace();

    }

}

    @Override

    public void onCreate() {

            super.onCreate();

    }

    @Override

    public IBinder onBind(Intent intent) {

            return null;

    }

    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

            return super.onStartCommand(intent, flags, startId);

    }

    @Override

    public void onDestroy() {

            super.onDestroy();

    }

}

@Override

public void onCreate() {

    super.onCreate();

    //加一点简单的动画 

    buttonScale = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.anim_float);

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    layoutParams =new WindowManager.LayoutParams();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

    }else {

            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

    }

    layoutParams.format = PixelFormat.RGBA_8888;

    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;

    layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |             WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

    layoutParams.width = ScreenUtils.dp2px(66);

    layoutParams.height = ScreenUtils.dp2px(66);

    layoutParams.x = ScreenUtils.getRealWidth() - ScreenUtils.dp2px(60);

    layoutParams.y = ScreenUtils.deviceHeight() *2 /3;

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

    ModeBean modeBean = (ModeBean) intent.getExtras().getSerializable(KEY_MODEL);

    LayoutInflater layoutInflater = LayoutInflater.from(this);

    floatView = layoutInflater.inflate(R.layout.view_float, null);

    RelativeLayout rlFloatParent =floatView.findViewById(R.id.rl_float_parent);

    rlFloatParent.startAnimation(buttonScale);

    TextView tvIndex =floatView.findViewById(R.id.tv_queue_index);

    tvIndex.setText(modeBean.title);

    floatView.findViewById(R.id.iv_close_float).setOnClickListener(v -> stopSelf());

    //修改悬浮球的滑动实现

    floatView.setOnTouchListener(new FloatingOnTouchListener());

    windowManager.addView(floatView, layoutParams);

    return super.onStartCommand(intent, flags, startId);

}

private class View.OnTouchListener {

    private int x;

    private int y;

    private long downTime;

    @SuppressLint("ClickableViewAccessibility")

    @Override

    public boolean onTouch(View view, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                        downTime = System.currentTimeMillis();

                        x = (int) event.getRawX();

                        y = (int) event.getRawY();

                        break;

                case MotionEvent.ACTION_MOVE:

                        int nowX = (int) event.getRawX();

                        int nowY = (int) event.getRawY();

                        int movedX = nowX -x;

                        int movedY = nowY -y;

                        x = nowX;

                        y = nowY;

                        layoutParams.x =layoutParams.x + movedX;

                        layoutParams.y =layoutParams.y + movedY;

                        windowManager.updateViewLayout(view, layoutParams);

                        break;

                case MotionEvent.ACTION_UP:

                        /* *

                        * 这里根据手指按下和抬起的时间差来判断点击事件还是滑动事件

                        * */

                        if ((System.currentTimeMillis() -downTime) <200) {

                                //检测应用在前台还是后台

                                if (AppUtils.isAppIsInBackground()) {

                                        AppUtils.moveToFront(CloseActivityUtils.activityList.get(CloseActivityUtils.activityList.size() -1).getClass());

                                } else {

                                        //检测栈顶是否为SecondActivity 不是就打开SecondActivity

                                      if (!CloseActivityUtils.activityList.get(CloseActivityUtils.activityList.size() -1)

                                                .getClass().getSimpleName().contains("SecondActivity")) {

                                        SecondActivity.launchActivity(CloseActivityUtils.activityList.get(CloseActivityUtils.activityList.size() -1));

                                }

                      }

            }

                        break;

            default:

                        break;

        }

           return false;

    }

}

        @Override

        public void onDestroy() {

            super.onDestroy();

            if (null ==floatView) {

                return;

               }

            windowManager.removeView(floatView);

            windowManager=null;

}

⑶ 关于Android动态布局添加和删除View的问题……

java">{

privateLinearLayoutlayout;
privateTextViewtextView;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=newLinearLayout(this);//变量layout是该Activity的成员变量(privateLinearLayoutlayout)
layout.setOrientation(LinearLayout.VERTICAL);//设置layout布局方向为垂直
setContentView(layout);

//接下来向layout中添加TextView
textView=newTextView(this);
textView.setText("ThisIsaTextView");
layout.addView(textView);
}
@Override
protectedvoidonResume(){
//TODOAuto-generatedmethodstub
layout.removeView(textView);
super.onResume();
}

}

但是Activity在启动的时候调用onCreate()之后也会调用onResume()方法,所以进入程序也看不到textview了

⑷ 如何实现一个 Android 端的富文本编辑器

  1. WebView + JavaScript;

  2. EditText + Span;

  3. scrollview + view;

  1. 效果图:

其他

在scrollview实现一些view的添加和删除,以及组件间的拼接,就可以实现一个很简单的可定制的富文本编辑器。

⑸ Android 怎么在程序运行过程中销毁View

只是不显示的话用View.setVisibility(boolean visibility)就行了,画面Layout会自动调整的。
另外画面用fragment来做,gridview和gallery分别做两个fragment,用FragmentManager来管理的话只会各生成一个实例,不会占太多内存

阅读全文

与androidremoveview相关的资料

热点内容
我的世界如何编程 浏览:84
vue反编译代码有问题 浏览:948
linuxshell字符串连接字符串 浏览:51
androidviewpager刷新 浏览:438
python编程计算平均分 浏览:678
加密数字货币市值查询 浏览:692
时尚商圈app怎么样 浏览:584
stacklesspython教程 浏览:138
用命令行禁用135端口 浏览:212
linux防火墙编程 浏览:627
pdf阅读器删除 浏览:979
考研人如何缓解压力 浏览:822
买电暖壶哪个app便宜 浏览:505
洛克王国忘记服务器了怎么办 浏览:782
为什么cf登录服务器没反应 浏览:695
服务器如何获取文件列表 浏览:672
creo五轴编程光盘 浏览:14
苹果app网络验证在哪里 浏览:14
博科清空命令 浏览:384
简爱英文pdf 浏览:376