如何从代码中设置android:layout_width =“match_parent”?

前端之家收集整理的这篇文章主要介绍了如何从代码中设置android:layout_width =“match_parent”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有不同的布局.一些由xml创建.其他一些人通过代码进行动态调整.当我在xml上时,我可以使用“wrap_content”值设置宽度或高度.如何获得相同的结果?这是我的dinamic TextView的片段.我需要删除“final int width = 440;”并获得相同的“wrap_content”值.怎么样?
  1. final int width = 440;
  2. final int height = textViewHeight;
  3. final int top = getNewTop(height);
  4.  
  5. FrameLayout.LayoutParams layoutParams;
  6. layoutParams = getLayoutParams(width,height,top);
  7.  
  8. TextView textView;
  9. textView = new TextView(_registerNewMealActivity);
  10. textView.setText(text);
  11. textView.setLayoutParams(layoutParams);
  12.  
  13. _frameLayout.addView(textView);

解决方法

试试如下:
  1. FrameLayout.LayoutParams layoutParams;
  2. layoutParams = getLayoutParams(LayoutParams.WRAP_CONTENT,top);
  3.  
  4. TextView textView;
  5. textView = new TextView(_registerNewMealActivity);
  6. textView.setText(text);
  7. textView.setLayoutParams(layoutParams);

猜你在找的Android相关文章