首页 > 解决方案 > 放在LinearLayout的右边

问题描述

我必须将秒 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>

标签: androidxmllayout-gravity

解决方案


您可以使用以下方法实现:

约束布局

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

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

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

您可以找到有关ConstraintLayout 和 RelativeLayout 之间差异如何使用约束(关系)的帖子

例子:

<?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>

线性布局


推荐阅读