Android:为什么layout_marginTop只与include标记一起工作?
我的android应用程序中有以下模板。当我使用android:layout_MarginTop=“20dip”时,我可以看到布局顶部的20dip空间Android:为什么layout_marginTop只与include标记一起工作?,android,android-layout,Android,Android Layout,我的android应用程序中有以下模板。当我使用android:layout_MarginTop=“20dip”时,我可以看到布局顶部的20dip空间 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" style="@style/lis
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
style="@style/list_buttom_single"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<LinearLayout android:gravity="center_vertical" android:paddingLeft="10dip" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0">
<TextView android:text="Login / Register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" style="@style/content_page_large_text" />
<TextView android:text="sample text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/subtitle" android:visibility="visible" style="@style/content_page_small_text" />
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/itemCount" android:visibility="gone" style="@style/content_page_large_count_text" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chevron" style="@style/list_buttom_chevron" />
</LinearLayout>
为什么对于“相同”的操作我会得到两种不同的行为,唯一的区别是一种是包含uisngxml,另一种是使用java
非常感谢
T因为布局参数仅在指定父视图时与视图关联。这就是为什么您应该使用另一个版本的:
View notesView = mInflater.inflate(R.layout.list_item_single, null);
((TextView) notesView.findViewById(R.id.title)).setText("Notes");
((TextView) notesView.findViewById(R.id.subtitle)).setText("102 notes");
btnContainer.addView(notesView);
View notesView = mInflater.inflate(R.layout.list_item_single, btnContainer, false);
...
btnContainer.addView(notesView);