【问题标题】:Place on the right in LinearLayout放在LinearLayout的右边
【发布时间】:2019-03-29 16:04:10
【问题描述】:

我必须将第二个 TextView 和 ImageView 放在右侧,但重力不会移动。我能怎么做?也许“重力”不合适? 谢谢!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/JuventusLogo"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JUVENTUS"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="REAL MADRID"/>
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/RealMadridLogo"/>

</LinearLayout>

【问题讨论】:

  • 输出应该是什么样子?

标签: android xml layout-gravity


【解决方案1】:

您可以使用:

约束布局

ConstraintLayout 允许您创建具有平面视图层次结构的复杂布局(没有嵌套视图组,例如您的示例中的LinearLayout)。它类似于RelativeLayout,因为所有视图都是根据兄弟之间的关系布局的。 ConstraintLayout 比RelativeLayout 具有更大的灵活性和性能。

ConstraintLayout 可用作支持库,因此您必须添加新的依赖项:

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

您可以找到有关differences between ConstraintLayout and RelativeLayout 或How to use constraint (relations) 的帖子

例子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/first_logo"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#F00"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/first_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JUVENTUS"
        app:layout_constraintBottom_toBottomOf="@id/first_logo"
        app:layout_constraintStart_toEndOf="@id/first_logo"
        app:layout_constraintTop_toTopOf="@id/first_logo" />

    <TextView
        android:id="@+id/second_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="REAL MADRID"
        app:layout_constraintBottom_toBottomOf="@id/second_logo"
        app:layout_constraintEnd_toStartOf="@id/second_logo"
        app:layout_constraintTop_toTopOf="@id/second_logo" />

    <ImageView
        android:id="@+id/second_logo"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="end"
        android:background="#00F"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>


线性布局

LinearLayout 是一个视图组,可将所有子级对齐在一个方向(如您的示例中垂直或水平)。您可以在单个子视图上设置android:layout_weight,以指定线性布局如何在其包含的视图之间划分剩余空间。

关于这个的帖子很多:

例子:

<?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:paddingLeft="16dp"
    android:paddingRight="16dp">

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#F00" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="JUVENTUS" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_gravity="end"
        android:layout_weight="1"
        android:gravity="center_vertical|end"
        android:text="REAL MADRID" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#00F" />

</LinearLayout>

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示 为什么 这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
  • @Zoe 添加。感谢您的关注。
猜你喜欢
  • 2023-03-23
  • 2023-04-06
  • 2015-03-29
  • 1970-01-01
  • 2021-01-04
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
相关资源
最近更新 更多