Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.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布局:组动态元素_Android_Android Layout_Layout - Fatal编程技术网

Android布局:组动态元素

Android布局:组动态元素,android,android-layout,layout,Android,Android Layout,Layout,我有一个图像和文本元素的视图。其中一些是动态的,所以我想将它们分组在一个“容器”(可能是线性布局?)中,所以当其中一些不在那里时,下面的元素可能有20dp的边距,而不是一个大的空白 这是布局图: 我的代码: (我要分组:phoneTitleTextView、homePhoneTextView和homeTitleTextView) 使用RelativeLayout必须指定每个视图相对于另一视图的位置。例如,要将phone\u 02\u title放在phone\u 01\u title下面,您

我有一个图像和文本元素的视图。其中一些是动态的,所以我想将它们分组在一个“容器”(可能是线性布局?)中,所以当其中一些不在那里时,下面的元素可能有20dp的边距,而不是一个大的空白

这是布局图:

我的代码: (我要分组:phoneTitleTextView、homePhoneTextView和homeTitleTextView)



使用
RelativeLayout
必须指定每个视图相对于另一视图的位置。例如,要将
phone\u 02\u title
放在
phone\u 01\u title
下面,您可以像这样使用下面的属性

<RelativeLayout 
    ...>

    <TextView
        android:id="@+id/phone_01_title"
        ... 
        />

    <TextView
        android:id="@+id/phone_02_title"
        android:layout_below="@+id/phone_01_title"
        ... 
        />

    ...

</RelativeLayout>

...
无论如何,如果您想在“电话”组和“地址”组之间设置一个边距,我建议您使用
android:paddingBottom=“”
。这将在列表下方放置空格,与组的行数无关

如果您想为缺少的视图留下一个“自由空间”(假设您有2/5部手机),我宁愿为容器和行设置一个固定的高度。例如,每个TextView的高度为40dp,容器为200dp(5x40 dp)

最后但并非最不重要的一点是,我还建议您对视图使用“setVisibility”,而不是动态添加它们,前提是您知道它们将在1到5之间。在XML布局中设置所有5个视图,然后以编程方式使用
view.setVisibility(view.VISIBLE)
view.setVisibility(view.INVISIBLE)
显示/隐藏其内容(但不显示其高度)


希望这些对你有帮助

你提到线性布局,你试过了吗?谢谢!!修复了我的问题,感谢SetVisibility的建议,我会使用它!很高兴我能帮忙:)
<RelativeLayout 
    ...>

    <TextView
        android:id="@+id/phone_01_title"
        ... 
        />

    <TextView
        android:id="@+id/phone_02_title"
        android:layout_below="@+id/phone_01_title"
        ... 
        />

    ...

</RelativeLayout>