代码大小相同的Android按钮
我想动态添加按钮,并使它们大小相同。按钮计数取决于单词的长度。当我说了很长的一句话时,它看起来很好: 但当我的单词只有一个字母时,它看起来是这样的: 这是我的代码:代码大小相同的Android按钮,android,button,dynamic,size,Android,Button,Dynamic,Size,我想动态添加按钮,并使它们大小相同。按钮计数取决于单词的长度。当我说了很长的一句话时,它看起来很好: 但当我的单词只有一个字母时,它看起来是这样的: 这是我的代码: public void loadButtons() { buttonsLayout = (LinearLayout) findViewById(R.id.buttons_Layout); answerButtons = new Button[word.length()]; for (int i =
public void loadButtons() {
buttonsLayout = (LinearLayout) findViewById(R.id.buttons_Layout);
answerButtons = new Button[word.length()];
for (int i = 0; i < word.length(); i++) {
answerButtons[i] = new Button(this);
answerButtons[i].setText(null);
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1f);
answerButtons[i].setLayoutParams(Params1);
answerButtons[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
buttonsLayout.addView(answerButtons[i]);
}
public void loadButtons(){
buttonsLayout=(LinearLayout)findViewById(R.id.buttons\u布局);
answerButtons=新按钮[word.length()];
for(int i=0;i
我怎样才能做到这一点呢?:)谢谢这是一个黑客解决方案,希望能奏效
public void loadButtons() {
buttonsLayout = (LinearLayout) findViewById(R.id.buttons_Layout);
answerButtons = new Button[word.length()];
LinearLayout.LayoutParams layoutParams = (word.length() == 1) ? new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT) : new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1f);
for (int i = 0; i < word.length(); i++) {
answerButtons[i] = new Button(this);
answerButtons[i].setText(null);
answerButtons[i].setLayoutParams(layoutParams);
answerButtons[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
buttonsLayout.addView(answerButtons[i]);
}
public void loadButtons(){
buttonsLayout=(LinearLayout)findViewById(R.id.buttons\u布局);
answerButtons=新按钮[word.length()];
LinearLayout.LayoutParams LayoutParams=(word.length()==1)?新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.MATCH_父项):新建LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_父项,1f);
for(int i=0;i
感谢您的快速回复。:)尽管如此,它并不能完全解决我的问题。如果我在大屏幕上运行它,例如用3个字母word,它会再次变得粗糙。类似这样:…它是绝对大小吗?如果我给它绝对大小,它不适合小屏幕。在图像上,前3个字母可能是绝对大小大小,但我认为图像的底部是一个嵌套的两行线性布局。您将使用多少个字母?当屏幕上的字母太多时,您希望发生什么,以便我可以尝试制作一个更动态的解决方案。