【问题标题】:Unable to Add a subview to a LinearLayout with "Wrap_Content"无法使用“Wrap_Content”将子视图添加到 LinearLayout
【发布时间】:2010-08-29 01:42:51
【问题描述】:
我正在使用数据构建ViewGroup 的元素。 ViewGroup 是 LinearLayouts,除了叶子,它们是 TextViews。因为 Data 绘制层次结构,所以我以编程方式动态地从各个 XML 文件中扩充 LinearLayouts 和 TextViews。
我的问题:
当我创建一个带有wrap_content 属性的LinearLayout,然后将TextView 或其他LinearLayout 充气到其中时,我无法让父母(或者足够多的父母)重绘,尊重新视图.这些LinearLayouts 似乎总是以 0 大小出现,因为当它们第一次被创建时(在它们的孩子被充气之前),它们什么都没有。
我尝试调用ViewGroup 的一些常用方法,但似乎没有任何效果。
【问题讨论】:
标签:
android
android-layout
android-ui
android-linearlayout
【解决方案1】:
我之前没有登录过。请在下面找到:顶部父 XML、子布局 XML、textView XML 和插入代码。感谢您帮助我!
顶级父 Xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tasks_view">
</LinearLayout>
子布局 XML
<LinearLayout
android:orientation="vertical"
android:id="@+id/task_line_tasks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
文本视图 XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="10pt"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:background="#EAADEA"
android:textColor="#000000"/>
插入代码:
inflater.inflate(R.layout.person_line, tasks_view);
newPersonLine = (LinearLayout) tasks_view.getChildAt(tasks_view.getChildCount()-1);
newPersonLine.setId(i);
newPersonName = (TextView)newPersonLine.getChildAt(0);
newPersonName.setId(0);
newPersonName.setText(people[i][0]);