使用重力或线性向左对齐LinearLayout中的图像

首先,这不是一个重复的问题 ,尽我所能,我尝试了所有(有很多)类似的问题。 这些问题的解决方案似乎是非常主观的,特定于特定情况。

我的布局目前显示如下。 黑色方块是图像(分别是徽标和身体),颜色代表每种布局:

布局的屏幕截图

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:padding="0px"
    android:layout_margin="0px"
    android:orientation="vertical">

    <LinearLayout
        android:layout_weight="16"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF"
        android:gravity="top|center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/logo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/logo"
            android:layout_gravity="top|center" />
    </LinearLayout>

    <LinearLayout
        android:layout_weight="4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00F"
        android:gravity="bottom|left"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/body"
            android:layout_gravity="bottom|left" />
    </LinearLayout>

</LinearLayout>

在这里你可以看到我有一个父线性布局,分成两个子线性布局。 这是因为我需要将图像在页面的该部分中进行不同的定位。

简而言之,我需要将徽标垂直对齐到顶部,并将身体水平对齐到左下角。

现在,我尝试了几件事情:

  • 使用RelativeLayout而不是Linear
  • 使用layout_gravity为LinearLayout和ImageView切换gravity ,以及排除各自的组合
  • 宽度和高度相当有自信的match_parent是我想要的,但我已经尝试了与wrap_content不同的组合
  • 我已经明白了:

  • gravity:top需要父视图使用orientation:horizontal
  • gravity:left要求父视图使用orientation:vertical
  • gravity适用于视图的儿童
  • linear_gravity应用了孩子如何与其父母对齐的方式
  • 在父linear_gravity上使用相同的gravity值,在子linear_gravity上使用linear_gravity可能会有相同的效果(当使用一个而不是另一个时)?
  • 希望这是足够的信息。 我很困难的时间围绕着这些布局如何运作。

    十分感谢你的帮助!


    我认为你的问题是你将图像视图的尺寸设置为match_parent 。 我会使用RelativeLayout,因为它似乎是最有效的(伪XML代码):

    RelativeLayout (width=match_parent, height=match_parent)
      ImageView (width=wrap_content, height=wrap_content, 
                 alignParentTop=true, centerHorizontal=true)
      ImageView (width=wrap_content, height=wrap_content, 
                 alignParentBottom=true, alignParentLeft=true)
    

    这里你不需要任何重力设置。 您可能需要根据图像大小来使用scaleType属性。

    链接地址: http://www.djcxy.com/p/93293.html

    上一篇: Left aligning images in LinearLayout using gravity or linear

    下一篇: In Android, how do I set margins in dp programmatically?