【问题标题】:two elements in linear layout are packed to center instead of to the sides线性布局中的两个元素被包装到中心而不是侧面
【发布时间】:2020-02-27 15:02:31
【问题描述】:

我有这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <com.google.android.libraries.onegoogle.account.particle.AccountParticle
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/part_a"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical|start" />
  <ImageView
      android:id="@+id/part_b"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginEnd="14dp"
      android:layout_marginRight="14dp"
      android:layout_gravity="center_vertical|end"
      app:srcCompat="@drawable/my_image" />
</LinearLayout>

我希望part_a 与右侧对齐,part_b 与左侧对齐。 (感谢android:layout_gravity)。

为什么我看到他们挤得很紧?

【问题讨论】:

  • 贴一些代码..
  • @MichaelStoddart 我的错,xml 没有被看到

标签: android android-layout android-linearlayout layout-gravity


【解决方案1】:

使用ConstraintLayout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.libraries.onegoogle.account.particle.AccountParticle
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/part_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
    <ImageView
        android:id="@+id/part_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="14dp"
        android:layout_marginRight="14dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/my_image" />
</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-11
    • 2013-01-09
    • 2011-03-20
    • 2012-03-07
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多