gpt4 book ai didi

Android Studio 改变 LinearLayout 大小

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:45 25 4
gpt4 key购买 nike

我想在我的程序中而不是在 xml 文件本身中更改线性布局的大小。我是初学者,也想在方法(公共(public))之外声明我的变量并在方法中初始化它,就像我用 EditText 和 LinearLayout 所做的那样。

我那样试过,但它说:

Error:(47, 81) error: incompatible types: android.view.ViewGroup.LayoutParams cannot be converted to android.widget.LinearLayout.LayoutParams 

但是我不知道如何处理这个错误:(

到目前为止我的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


public EditText input_chat;
public LinearLayout layout_chat_input;
public LinearLayout.LayoutParams layout_chat_input_params;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toast.makeText(getApplicationContext(),"onCreate",Toast.LENGTH_SHORT).show();

input_chat = (EditText) findViewById(R.id.input_chat);
layout_chat_input = (LinearLayout) findViewById(R.id.layout_chat_input);
layout_chat_input_params = new LinearLayout.LayoutParams(layout_chat_input.getLayoutParams());

input_chat.addTextChangedListener(new TextWatcher()
{

public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (input_chat.getLineCount() > 0 && input_chat.getLineCount() < 5)
{

layout_chat_input_params = layout_chat_input.getLayoutParams();
layout_chat_input.setLayoutParams();
}
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
});
}
}

最佳答案

尝试:

public void onTextChanged(CharSequence s, int start, int before, int count){

if (input_chat.getLineCount() > 0 && input_chat.getLineCount() < 5){
layout_chat_input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}

layout_chat_input.setLayoutParams(layout_chat_input_params);

关于Android Studio 改变 LinearLayout 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614010/

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