gpt4 book ai didi

Android:为什么 layout_marginTop 只能与 include 标签一起使用?

转载 作者:行者123 更新时间:2023-11-29 22:22:49 25 4
gpt4 key购买 nike

我的 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>

但是,当我尝试使用 LayoutInflater 以编程方式执行此操作时,我失去了这个 20dip 空间。

        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);

为什么我对“相同”操作有两种不同的行为,唯一的区别是一种是包含 uisng xml,另一种是使用 java.lang.

非常感谢

最佳答案

因为布局参数只有在指定父 View 的情况下才会与 View 相关联。这就是为什么你应该使用另一个版本的 inflate() :

View notesView = mInflater.inflate(R.layout.list_item_single, btnContainer, false);
...
btnContainer.addView(notesView);

关于Android:为什么 layout_marginTop 只能与 include 标签一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6694412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com