简体   繁体   English

如何以编程方式更改 LinearLayout 以适应输入到 EditText 的数字而不移动布局的其他部分?

[英]How to change LinearLayout programmaticaly to fit numbers entered to EditText without moving other parts of layout?

Im trying to create LinearLayout programmaticaly and I would want to allow user to put number range.我正在尝试以编程方式创建 LinearLayout,我希望允许用户输入数字范围。 Now it looks like that:现在看起来像这样:

在此处输入图像描述

But when I try to enter more digits eg.但是当我尝试输入更多数字时,例如。 100, 101 or 3,50 it dissapears. 100、101 或 3,50 它消失了。

在此处输入图像描述

I guess there is not enough space for it to be shown, but I can't figure out what is wrong.我想没有足够的空间来显示它,但我不知道出了什么问题。 Generally I don't want to move + and - buttons when the user enters some values so I guess it should be hardcoded.通常我不想在用户输入一些值时移动 + 和 - 按钮,所以我想它应该是硬编码的。 There would be up to 5-6 digits only, so I need space just for it, but as I said, I can't find the place, where I can change it as my changes eiter move entire layout or doesn't do anything.最多只有 5-6 位数字,所以我只需要空间,但正如我所说,我找不到可以更改它的地方,因为我的更改要么移动整个布局,要么什么都不做.

Below is my code:下面是我的代码:

LinearLayout horizontalLayout = new LinearLayout(mContext);
            LinearLayout titleLayout = new LinearLayout(mContext);
            LinearLayout countLayout = new LinearLayout(mContext);
            ImageButton buttonAdd = new ImageButton(mContext);
            ImageButton buttonSub = new ImageButton(mContext);
            TextView titleTextView = new TextView(mContext);
            EditText countEditText = new EditText(mContext);

            final int[] currentCount = {defaultValue};

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
            LinearLayout.LayoutParams utilParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
            horizontalLayout.setLayoutParams(params);

            utilParams.gravity = Gravity.CENTER_VERTICAL;
            titleLayout.setOrientation(LinearLayout.HORIZONTAL);
            titleLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
            titleLayout.setLayoutParams(linearLayout);

            countLayout.setOrientation(LinearLayout.HORIZONTAL);
            countLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
            countLayout.setLayoutParams(linearLayout);

            utilParams.setMargins(0,0,pxFromDp(mContext, 16f),0);
            titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
            titleTextView.setText(title);
            titleTextView.setLayoutParams(utilParams);
            titleLayout.addView(titleTextView);

            utilParams.setMargins(pxFromDp(mContext, 16f),0,pxFromDp(mContext, 16f),0);
            buttonSub.setImageResource(R.drawable.ic_remove);
            buttonSub.setLayoutParams(utilParams);
            buttonSub.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
            buttonSub.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
            countLayout.addView(buttonSub);

            countEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            countEditText.setText(String.valueOf(defaultValue));
            countEditText.setLayoutParams(linearLayout);
            countEditText.setGravity(Gravity.CENTER);
            countLayout.addView(countEditText);

            buttonAdd.setImageResource(R.drawable.ic_add_24);
            buttonAdd.setLayoutParams(utilParams);
            buttonAdd.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
            buttonAdd.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
            countLayout.addView(buttonAdd);

            horizontalLayout.addView(titleLayout);
            horizontalLayout.addView(countLayout);

What you can do is to create separate LinearLayout.LayoutParams for the edittext and add addTextChangedListener to that edit text and on onTextChanged write switch statement to increase the weight of the edittext on increase of length of the input number.您可以做的是为编辑文本创建单独的 LinearLayout.LayoutParams 并将 addTextChangedListener 添加到该编辑文本,并在 onTextChanged 上编写 switch 语句以增加输入数字长度增加时编辑文本的权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM