android - 放在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>
解决方案
您可以使用以下方法实现:
约束布局
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>
推荐阅读
- reactjs - Firebase 托管:为什么 firebase 将我的所有源代码托管到那里托管?我怎样才能仅限于“构建”文件夹?
- python - Django如何将views.py中的参数传递给另一个文件中的websocket“on_open”函数
- mongodb - Bash在变量中添加单引号
- scala - 紧凑的单一方法与重复
- ios - 如何在 Swift 中使用 GRDB 创建多对多关联?
- animation - Three.js 缓冲几何动画
- delphi - 启用协议 HTTP2 Delphi 11
- c# - 帖子中的 Json 对象未被识别为 Web api 中的对象
- c# - 将 GroupJoin 结果转换为 linq 中的 List 到 sql Lambda 表达式 C#
- php - 复杂的 SQL / Laravel Eloquent 查询