【问题标题】:Imageviews not centering when using layout_weights使用 layout_weights 时图像视图不居中
【发布时间】:2012-07-03 03:50:10
【问题描述】:

我正在尝试制作一个水平菜单栏,但是由于某种原因,当我使用权重时,它们不会水平对齐,有什么建议吗?

<LinearLayout
    android:id="@+id/bottombuttonslayout"
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:layout_alignParentBottom="true"
    android:weightSum="1.0" >

<LinearLayout
    android:id="@+id/listSlotBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".20"
    android:gravity="center" >

    <ImageView
        android:id="@+id/listSlot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="false"
        android:background="@drawable/list" />
</LinearLayout>

<LinearLayout
    android:id="@+id/leftArrorSlotBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight=".20" >

    <ImageView
        android:id="@+id/leftArrowSlot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="false"
        android:background="@drawable/left_arrow" />
</LinearLayout>

<LinearLayout
    android:id="@+id/playSlotBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight=".20" >

    <ImageView
        android:id="@+id/playSlot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="false"
        android:background="@drawable/playing" />
</LinearLayout>

<LinearLayout
    android:id="@+id/rightArrowSlotBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight=".20" >

    <ImageView
        android:id="@+id/rightArrowSlot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="false"
        android:background="@drawable/right_arrow" />
</LinearLayout>

这就是它的样子,它们是垂直对齐的。不是水平的:

编辑

这是遵循第一个建议:

【问题讨论】:

  • 它们不会水平对齐,因为android:layout_gravity="center" 仅适用于水平方向的LinearLayout 的垂直方向(对于垂直方向的LinearLayout,反之亦然)。您是否尝试在嵌套的 LinearLayouts 上添加 android:gravity="center"?或者,您可以随时将它们换成FrameLayoutRelativeLayout 之类的东西,这样您就可以更好地控制定位。

标签: android android-layout android-linearlayout android-imageview


【解决方案1】:

每个图像视图都需要android:gravity="center"

您正在将包含的线性布局居中...我不明白您为什么要使用所有这些...您应该使用一个,如下所示:

<LinearLayout 
    android:orientation="horizontal"
    android:id="@+id/bottombuttonslayout" 
    android:layout_width="match_parent" 
    android:layout_height="58dp" 
    android:layout_alignParentBottom="true" > 
    <ImageView 
        android:id="@+id/listSlot" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="false" 
        android:background="@drawable/list"
        android:gravity="center"/>
    <ImageView 
        android:id="@+id/leftArrowSlot" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="false" 
        android:background="@drawable/left_arrow"
        android:gravity="center" 
        android:layout_weight="1" /> 
    <ImageView 
        android:id="@+id/playSlot" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="false" 
        android:background="@drawable/playing"
        android:gravity="center" 
        android:layout_weight="1" /> 
    <ImageView 
        android:id="@+id/rightArrowSlot" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="false" 
        android:background="@drawable/right_arrow"
        android:gravity="center" 
        android:layout_weight="1" /> 
</LinearLayout> 

【讨论】:

  • 按照您提供的内容编译到上面的内容。我会将图片添加到我的帖子中。
  • 搞错了。图像视图的所有宽度都需要设置为“0dp”。而且我不确定您是否应该保留android:adjustViewBounds="false" 或将其设置为true...作为最后的手段,恢复您所拥有的,但将android:gravity="center" 添加到每个图像视图。
  • 好吧,我尝试了所有提到的,但没有任何效果。第一部分并没有奇怪地改变任何东西,并且根据您的建议重用我的旧代码只是保持它在我的第一张图像中显示的方式。很混乱。它垂直居中,但不是水平居中。
猜你喜欢
  • 2011-08-19
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多