Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:为什么layout_marginTop只与include标记一起工作?_Android_Android Layout - Fatal编程技术网

Android:为什么layout_marginTop只与include标记一起工作?

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

我的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/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);