Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有文本视图,我想使用权重和宽度,但我想将宽度作为 0dp 传递,如果我在布局参数中将宽度设置为 0,它不会在屏幕上呈现。
如何通过代码动态地将宽度设置为 0dp
我什至尝试将宽度指定为 LayoutParams.Wrap_Content,但这也没有奏效
LinearLayout.LayoutParams tv_params_level = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.1f);
如果通过 xml 将 TextView 的宽度设置为0dp,则必须将其权重设置为1或其他大于 0 的值。以编程方式设置宽度:
LinearLayout.LayoutParams tv_params_level = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0);
然后将此布局参数设置为您的 TextView:
tv.setLayoutParams(tv_params_level);