gpt4 book ai didi

java - Android EditText动态高度以编程方式

转载 作者:行者123 更新时间:2023-11-30 02:19:59 26 4
gpt4 key购买 nike

我的 EditText 有点问题。我想让它自动增加高度。我发现其他人要求它:like this one

我的问题是我没有使用 XML 文件。我只是使用我的 Java 代码:

public class CommentatorView extends LinearLayout {

//Variablen um den Taschenrechner anzupassen
final static float KLEIN = 0.75f;
final static float MITTEL = 1.0f;
final static float GROS = 1.5f;
final static float GROS_1 = 2.0f;
final static float GROS_2 = 3.0f;
final static float GROS_3 = 4.0f;


CommentatorListener clickListener;
Commentator goTo;
EditText commentField;
public CommentatorView(Commentator goTo) {
super(goTo.getContext());

this.setOrientation(LinearLayout.VERTICAL);
this.goTo = goTo;
clickListener = new CommentatorListener(this, goTo);

commentField = new EditText(this.getContext());
commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
commentField.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

commentField.setMinHeight(120);
this.addComponent();
}

private void addComponent() {

this.addView(this.commentField);

LinearLayout linearLayout = new LinearLayout(this.getContext());

LayoutParams buttonParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
buttonParams.weight = 1;
buttonParams.setMargins(1, 0, 1, 0);

Button btnSave = new Button(this.getContext());
btnSave.setOnClickListener(this.clickListener);
btnSave.setTypeface(null, Typeface.BOLD);
btnSave.setTextSize(18);
btnSave.setText("Speichern");
btnSave.setTag("save");
linearLayout.addView(btnSave, buttonParams);

Button btnCancel = new Button(this.getContext());
btnCancel.setOnClickListener(this.clickListener);
btnCancel.setTypeface(null, Typeface.BOLD);
btnCancel.setText("Abbrechen");
btnCancel.setTag("no");
btnCancel.setTextSize(18);

linearLayout.addView(btnCancel, buttonParams);

this.addView(linearLayout);
}

public int getWantedNumber() {
int i = Integer.parseInt(commentField.getText().toString());
return i;
}

public boolean isNumber() {
try {
int d = Integer.parseInt(commentField.getText().toString());
Log.i("WTG", "" + d);
} catch (NumberFormatException nfe) {
Log.e("WTG", "Cast failed");
return false;
}
return true;
}}

有什么方法可以只用我的 java 代码自动增加高度吗?

提前致谢!

最佳答案

而不是做

commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

如果您希望它与您的 parent 一样高,请使用 MATCH_PARENT 作为高度。

commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

否则你可以使用重量。

commentField.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));

编辑

如果您希望 edittext 包装其内容但在必要时展开,请查看此问题 Create a multiline EditText programatically

关于java - Android EditText动态高度以编程方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com