如何为Android设置动态大小';你的观点是什么?
我试图设置最小尺寸,但仍有一个视图完全占据了主导地位。我的正常视图如下所示: 这两个值来自服务器。但是如果“hello”的大小变长,那么数字字符串就会消失 我的代码如下所示:如何为Android设置动态大小';你的观点是什么?,android,json,android-layout,Android,Json,Android Layout,我试图设置最小尺寸,但仍有一个视图完全占据了主导地位。我的正常视图如下所示: 这两个值来自服务器。但是如果“hello”的大小变长,那么数字字符串就会消失 我的代码如下所示: if (jsonDataViewType.get(i).toString().equals("editBox")) { editText = new EditText(context); editText.setMinLines(1);
if (jsonDataViewType.get(i).toString().equals("editBox")) {
editText = new EditText(context);
editText.setMinLines(1);
editText.setMinimumWidth(10);
linearLayoutHorizontal.addView(editText);
editText.setText(jsonDataValue.get(i));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
editText.setGravity(Gravity.CENTER_HORIZONTAL);
editText.setLayoutParams(params);
}
else if(jsonDataViewType.get(i).toString().equals("button")) {
helloButton = new Button(context);
// helloButton.setMinLines(1);
linearLayoutHorizontal.addView(helloButton);
helloButton.setText(jsonDataValue.get(i));
// testing
// helloButton.setText("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllooooooooooo");
}
我正在设置minLines和minWidth,但是您可以看到它仍然不起作用。所以您的问题是设置
setMinLines(1)
是一个下限设置,这意味着至少要使这一行变长。如果你想控制显示多少,你需要setMaxLines(1)
和同样的宽度设置setMinWidth(10)
意味着至少将其设为10宽,所以setMaxWidth(10)
你可能想考虑在按钮文本中添加椭圆大小,如果你想显示更多的文本,但是它被切断了。
更新 如果你想让他们坐在一起:// Linear Layout has weight sum of 3
linearLayoutHorizontal.setWeightSum(3.0f);
// Lets EditText take up 2/3 of view
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2.0f);
editText.setLayoutParams(params);
// Button gets the other 1/3
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
helloButton.setLayoutParams(params2);
尝试设置
EditView和按钮的layout\u width=0
。然后将布局权重设置为0.8和0.2
因此,这两个视图将填充父视图的宽度。现在,您的EditView已超出屏幕左侧。但我不知道最大长度可能是多少,因为它来自服务器。这就是为什么我想设置最小值而不是最大值。那么你要把它们添加到线性布局中吗?您希望它们如何显示?一个挨着另一个?所有的东西都是线性的。是的,hello和数字字符串应该挨在一起。我想我更喜欢你的省略想法。这个更新的解决方案将做什么?嘿,没有editText.setLayout\u width这样的东西。。。我将如何务实地做?