【问题标题】:Change the height of a view added dynamically更改动态添加的视图的高度
【发布时间】:2014-12-19 07:17:54
【问题描述】:

我在一个 android 中工作,我正在向线性布局动态添加一个视图。而且我从ViewTreeObserver 动态添加了视图的高度,但是在我在ViewTreeObserver 内实用地设置高度之后它并没有改变。请查看我的代码

onCreate

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.wwww);

            ScrollView parentScrollView = (ScrollView) findViewById(R.id.parentscroll);

            parentScrollView.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    findViewById(R.id.subscroll).getParent()
                            .requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            });

            ((ScrollView) findViewById(R.id.subscroll))
                    .setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {

                            v.getParent().requestDisallowInterceptTouchEvent(true);
                            return false;
                        }
                    });

            new TESTT().execute();

            final LinearLayout tv = (LinearLayout) findViewById(R.id.subscrollLinearLayout);
            ViewTreeObserver vto = tv.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                public void onGlobalLayout() {


            if (tv.getChildAt(0) != null) {
                int btnsize = tv.getChildAt(0).getHeight();
                ScrollView scrolllayout = (ScrollView) findViewById(R.id.subscroll);
                scrolllayout.getLayoutParams().height = btnsize * 2;
            }

                }
            });        
        }

异步任务:

private class TESTT extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(4000);//Time taken to consume the web service. If there
                                  // is no delay the code work correct
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);


            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final LinearLayout layout = (LinearLayout) findViewById(R.id.subscrollLinearLayout);
            List<String> mSempLog = new ArrayList<String>();

            mSempLog.add("TEST1");
            mSempLog.add("TEST2");
            mSempLog.add("TEST3");
            mSempLog.add("TEST4");
            mSempLog.add("TEST5");
            mSempLog.add("TEST6");
            mSempLog.add("TEST7");
            mSempLog.add("TEST8");



            for (int i = 0; i < 10; i++) {

                LinearLayout service_row = (LinearLayout) inflater.inflate(
                        R.layout.summary_list_row, null);

                TextView mServiceSlNo = (TextView) service_row
                        .findViewById(R.id.summary_list_textslNo);

                TextView mServiceText = (TextView) service_row
                        .findViewById(R.id.summary_list_textService);

                TextView mStartDate = (TextView) service_row
                        .findViewById(R.id.summaryview_list_initiated_text);

                TextView mEndDate = (TextView) service_row
                        .findViewById(R.id.summaryview_list_completed_text);

                TextView mFullyEngaged = (TextView) service_row
                        .findViewById(R.id.summaryview_fully_count_text);

                TextView mProgress = (TextView) service_row
                        .findViewById(R.id.summaryview_listentry_TextView);

                LinearLayout mServiceLinearLayout = (LinearLayout) service_row
                        .findViewById(R.id.summary_list_sercicelayout);

                mServiceSlNo.setText(Integer.toString(i + 1) + ".");
                mServiceText.setText("fdgdfgdfg");
                mStartDate.setText("fdgdfgdfg");

                mFullyEngaged.setText("fdgdfgdfg");

                mServiceLinearLayout.setTag(i);
                layout.addView(service_row, i);

            }

        }

    }

【问题讨论】:

    标签: android android-layout android-linearlayout android-custom-view


    【解决方案1】:

    你想添加的都有一些布局,在 layout.xml 文件中制作

    并使用这种方法在您的父布局中添加一个新视图。

    LinearLayout lay_toAddItems = (LinearLayout) rootView
                            .findViewById(R.id.lay_toAddItems);
    View hiddenInfo = orderDetails.getLayoutInflater().inflate(
                            R.layout.include_items_invieworder, lay_toAddItems,
                            false);
    lay_toAddItems.addView(hiddenInfo);
    
    • rootview = 父布局。
    • lay_toAddItems = 您要添加的位置
    • 新视图 include_items_invieworder = 您的新布局。

    编辑部分

    LinearLayout layoutDialog = (LinearLayout) findViewById(R.id.lay_forgotpassword);
    
    layoutDialog.setLayoutParams(new FrameLayout.LayoutParams(dialogWidth*3/4+dialogWidth*1/8,LayoutParams.WRAP_CONTENT));
    

    【讨论】:

    • 感谢您的回答,但您的回答与我的问题不符。
    • 为什么,我在我的项目中使用了这种方法。
    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2011-02-14
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多