【问题标题】:Android layout - image pushes view awayAndroid布局 - 图像将视图推开
【发布时间】:2023-04-11 00:46:01
【问题描述】:

这是我的布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_screen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:baselineAligned="false"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/bottleImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_margin="10dp"
        android:src="@drawable/icon_food_milk" />

    <ImageView
        android:id="@+id/diaperImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_margin="10dp"
        android:src="@drawable/icon_store_diaper" />

    <android.opengl.GLSurfaceView
        android:id="@+id/glview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginTop="60dp"
        android:windowIsTranslucent="true" />

</LinearLayout>

在应用程序 onCreate 我隐藏两个图像视图,然后在必要时显示它们。但是当我这样做时,GLSurfaceView 会向右移动图像视图的宽度。

【问题讨论】:

  • 试试这个:.... android:layout_gravity="bottom | left"
  • 因为您的 Parent android:orientation="horizo​​ntal" 将其更改为垂直。

标签: android android-layout layout imageview


【解决方案1】:

这是因为,您使用 LinearLayouthorizontal 方向。当你让你的 imageViews 可见时,这个 viewGroup 中的所有视图都将尝试水平适应。在这种情况下,如果要进行定位,请使用 RelativeLayoutalignParentBottom = true

【讨论】:

    【解决方案2】:

    你的父布局是线性布局,它的方向是

     android:orientation="horizontal"
    

    这将始终处于水平位置

    |    |  
    view1 view2....
    |    |
    

    将其更改为 `android:orientation="vertical"

    那么你的观点应该是

    view 1
    view 2
    .
    .
    

    【讨论】:

    • 如果我将其更改为“垂直”,则 GLSurfaceView 会向下移动而不是向左移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多