【问题标题】:How to Align three views horizontally with intermediate views?如何将三个视图与中间视图水平对齐?
【发布时间】:2018-08-17 11:43:51
【问题描述】:

我想垂直对齐 3 个布局,以便三个文本视图中的每一个都恰好位于另一个下方。它有中间视图。 所以我不能使用相对布局来使用属性 layout_below。虽然我设法将上述两种布局与线性布局和权重对齐,但第三种布局由一个图像视图组成,并且图像视图位于中心。 这是我的代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_marginStart="5dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="@string/status_heading"
            android:textSize="11sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="15dp"


            android:layout_weight="0.3"
            android:src="@drawable/my_drawable" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="test"
            android:textSize="15sp" />
    </LinearLayout>

当有 3 个文本视图时,它可以正常工作,但是这个图像视图会以某种方式到达自身的中心并且没有正确对齐。 谢谢你:)

【问题讨论】:

  • 你需要那个图片占据整个imgaview区域吗?
  • 请在 LinearLayout 中使用重力作为“中心”。
  • 不,它是一个小图像,我需要它靠近开始的地方。我可以使用填充,但这不是一个通用的解决方案。 @Suraj
  • @SaurabhVadhva 是的,我已经做过但没用
  • @Pritish 尝试使用android:scaleType="fitStart",正如您所说的需要图像才能启动。

标签: android android-layout android-linearlayout android-view


【解决方案1】:

只需将Image 与任何其他Layout 包裹起来,这样它就不会居中而是出现在开头:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:gravity="start"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="@string/status_heading"
        android:textSize="11sp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="15dp"
        android:layout_weight="0.3">

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/my_drawable" />
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="test"
        android:textSize="15sp" />
</LinearLayout>

【讨论】:

    【解决方案2】:

    试试这个

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="start"
        android:layout_marginStart="5dp"
        android:orientation="vertical"
        android:weightSum=".9">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:text="@string/status_heading"
            android:textSize="11sp" />
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:src="@drawable/ic_launcher_foreground" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:text="test"
            android:textSize="15sp" />
    </LinearLayout>
    

    你可以根据你的设计改变 weight 和 weightSum

    【讨论】:

      【解决方案3】:

      文本视图的高度是换行内容,因此大小将取决于您的情况不同的文本大小。现在图像的高度是在你的情况下定义的,所以显然它不会对齐。

      你可以做的是给父视图高度和布局重力作为中心。

      类似的东西 -

      <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="20dp"
              android:gravity="start"
              android:layout_marginStart="5dp"
              android:orientation="horizontal">
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="0.3"
                  android:layout_gravity="center"
                  android:text="@string/app_name"
                  android:textSize="11sp" />
      
              <ImageView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="0.3"
                  android:src="@drawable/d"
                  android:layout_gravity="center"
                  android:scaleType="centerInside"/>
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="0.3"
                  android:layout_gravity="center"
                  android:text="test"
                  android:textSize="15sp" />
          </LinearLayout>
      

      您可以将重力添加到文本视图的中心以获得适当的水平空间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-09
        • 2019-04-10
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 2013-04-25
        • 2023-03-31
        相关资源
        最近更新 更多