【问题标题】:Linearlayout spacing in AndroidAndroid中的线性布局间距
【发布时间】:2014-01-23 07:01:41
【问题描述】:

这是我的布局:

文本视图

IMAGEVIEW(可选)

LINEARLAYOUT - 我动态添加按钮

LINEARLAYOUT - 两个按钮并排(左键和右键)

我需要做些什么来确保底部的两个线性布局固定在屏幕底部,而不管它们可能占用多少空间? IE。第一个线性布局可能有 3 个按钮并占据一半以上的屏幕,这没关系。它只需要在最后一个线性布局中的左/右按钮上方,它固定在底部。

然后我希望我的 TextView 和我的 ImageView 在剩余空间中垂直居中。如果没有图像,ImageView 将被设置为不可见,因此它只能是需要居中的文本视图。

我一直在玩 android:gravity="bottom", android:layout_height="0dip"/android:layout_weight="1" (我后来意识到这只会给 text/imageview 50% 和 50 % 到 2 个线性布局),但我无法得到我想要的结果。

任何建议表示赞赏。

【问题讨论】:

  • 向我们展示您的确切代码..
  • 我需要做些什么来确保底部的两个线性布局固定在屏幕底部 - 将所有视图包装在RelativeLayout 中,@带有两个按钮的 987654323@ 将具有规则 layout_alignParentBottom,另一个 LinearLayout 将在第一个 LinearLayout(layout_above) 之上。将其余两个视图包裹在另一个布局中,并将其放在上边距和动态 LinearLayout 上方。
  • 嗨 Luksprog,我已经尝试过这个(以及我们所建议的)并且它很接近,但我发现顶部的文本视图由于某种原因是顶部对齐和左对齐 no有什么关系。理想情况下,我希望文本/图像居中对齐,但如果没有图像(即 Visibility.GONE),则文本仍居中对齐。我使用的代码是这样的:pastebin.ca/2580989

标签: android android-linearlayout


【解决方案1】:

你必须接受RelativeLayout。 您可以更好地控制视图的相对位置,如下所示:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
>
    <TextView 
        android:id="@+id/textView"
        android:layout_above="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <ImageView 
        android:id="@+id/imageView"
        android:layout_above="@+id/ll_1"
        android:layout_centerVertical="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <LinearLyout 
        android:id="@+id/ll_1"
        android:layout_above="@+id/ll_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <LinearLyout 
        android:id="@+id/ll_2"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />


</RelativeLayout>

【讨论】:

  • 您好,我已经按照您的示例(以及 Luksprog 所说的)进行了操作,它很接近但并不完全存在。我已经解释了在回复 Luksprog 的回答时发生的事情。谢谢。
  • 试试这个:将LinearLayoutlayout_widthlayout_height 属性更改为wrap_content。还将android:layout_centerVertical="true" 更改为android:layout_centerInParent="true"。这是一篇关于如何使用gravity 属性来查看视图内容的帖子:stackoverflow.com/questions/13965883/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多