導航:首頁 > 操作系統 > 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相關的資料

熱點內容
壓縮機風扇電機轉速慢 瀏覽:88
文件伺服器如何查看訪問人員 瀏覽:127
絕佳買賣指標加密 瀏覽:758
git分支編譯 瀏覽:156
51單片機c語言應用程序設計實例精講 瀏覽:562
華為安卓手機編譯器 瀏覽:48
怎樣在打開微信前加密 瀏覽:666
旺旺聊天記錄怎麼加密 瀏覽:413
王安憶長恨歌pdf 瀏覽:621
mobile文件夾可以卸載嗎 瀏覽:282
什麼是2通道伺服器 瀏覽:346
mc正版怎麼開伺服器地址 瀏覽:408
樂高解壓朋友圈 瀏覽:14
linux軟raid性能 瀏覽:368
貼片機編程軟體下載 瀏覽:360
mooc大學樂學python答案 瀏覽:408
怎麼投訴途虎app 瀏覽:37
安卓重力感應怎麼關 瀏覽:721
我的世界ios怎麼建伺服器地址 瀏覽:759
伺服器埠ip都是什麼意思 瀏覽:263