RelativeLayout 对齐不起作用
RelativeLayout alignment does not work
我正在尝试显示有关足球比赛的信息。我想要这样的图片:
在每一行中,我希望按此顺序
- 主队标志
- 主队名称
- 比赛时间
- 客队名称
- 客队标志
我正在为单行使用以下 xml 代码:
<?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="60dp"
android:padding="4dp">
<TextView
android:id="@+id/time"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="18:00"
android:textAlignment="center" />
<ImageView
android:id="@+id/homeTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/awayTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/homeTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/homeTeamLogo"
android:layout_toLeftOf="@id/time"
android:layout_toRightOf="@id/homeTeamLogo"
android:layout_toStartOf="@id/time"
android:paddingLeft="4dp"
android:text="home team" />
<TextView
android:id="@+id/awayTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/time"
android:layout_toLeftOf="@id/awayTeamLogo"
android:layout_toRightOf="@id/time"
android:gravity="right"
android:layout_toStartOf="@id/awayTeamLogo"
android:paddingRight="4dp"
android:text="away team" />
</RelativeLayout>
在 Android Studio 的预览中,我看到了正确的行为:
但是模拟器和真机的结果都是错误的:
我不明白原因,请问有人能帮帮我吗?
在我的 viewHolder 中,我在 bind 方法中设置了所有数据,我对图像使用 glide:
public void bind(RequestManager glide, final Match match) {
mTimeTV.setText(DateHelper.getMatchTime(match.getDate()));
glide.load(match.getHomeTeamLogoPath())
.into(mHomeTeamLogoIV);
glide.load(match.getAwayTeamLogoPath())
.into(mAwayTeamLogoIV);
mHomeTeamTV.setText(match.getHomeTeamName());
mAwayTeamTV.setText(match.getAwayTeamName());
}
应要求我尝试将颜色作为背景,这是预览:
这是结果:
尝试将您的 xml 替换为 Constraint Layout :
<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"
android:padding="4dp">
<ImageView
android:id="@+id/homeTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/homeTeam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="4dp"
android:text="home team"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/time"
app:layout_constraintStart_toEndOf="@+id/homeTeamLogo"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/time"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="18:00"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/awayTeam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:gravity="right"
android:paddingRight="4dp"
android:text="away team"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/awayTeamLogo"
app:layout_constraintStart_toEndOf="@+id/time"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/awayTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我正在尝试显示有关足球比赛的信息。我想要这样的图片:
在每一行中,我希望按此顺序
- 主队标志
- 主队名称
- 比赛时间
- 客队名称
- 客队标志
我正在为单行使用以下 xml 代码:
<?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="60dp"
android:padding="4dp">
<TextView
android:id="@+id/time"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="18:00"
android:textAlignment="center" />
<ImageView
android:id="@+id/homeTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/awayTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/homeTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/homeTeamLogo"
android:layout_toLeftOf="@id/time"
android:layout_toRightOf="@id/homeTeamLogo"
android:layout_toStartOf="@id/time"
android:paddingLeft="4dp"
android:text="home team" />
<TextView
android:id="@+id/awayTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/time"
android:layout_toLeftOf="@id/awayTeamLogo"
android:layout_toRightOf="@id/time"
android:gravity="right"
android:layout_toStartOf="@id/awayTeamLogo"
android:paddingRight="4dp"
android:text="away team" />
</RelativeLayout>
在 Android Studio 的预览中,我看到了正确的行为:
但是模拟器和真机的结果都是错误的:
我不明白原因,请问有人能帮帮我吗?
在我的 viewHolder 中,我在 bind 方法中设置了所有数据,我对图像使用 glide:
public void bind(RequestManager glide, final Match match) {
mTimeTV.setText(DateHelper.getMatchTime(match.getDate()));
glide.load(match.getHomeTeamLogoPath())
.into(mHomeTeamLogoIV);
glide.load(match.getAwayTeamLogoPath())
.into(mAwayTeamLogoIV);
mHomeTeamTV.setText(match.getHomeTeamName());
mAwayTeamTV.setText(match.getAwayTeamName());
}
应要求我尝试将颜色作为背景,这是预览:
这是结果:
尝试将您的 xml 替换为 Constraint Layout :
<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"
android:padding="4dp">
<ImageView
android:id="@+id/homeTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/homeTeam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="4dp"
android:text="home team"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/time"
app:layout_constraintStart_toEndOf="@+id/homeTeamLogo"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/time"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="18:00"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/awayTeam"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:gravity="right"
android:paddingRight="4dp"
android:text="away team"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/awayTeamLogo"
app:layout_constraintStart_toEndOf="@+id/time"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/awayTeamLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />