首页 > 解决方案 > 将 ImageView 固定到 LinearLayout 中最右侧的位置

问题描述

所以,这是我目前的LinearLayout
在此处输入图像描述

使用 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="8dp">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:id="@+id/crime_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Crime Title" />

        <TextView
            android:id="@+id/crime_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Crime date" />
    </LinearLayout>

    <ImageView
        android:id="@+id/crime_police"
        android:layout_width="117dp"
        android:layout_height="50dp"
        android:layout_gravity="fill_horizontal"
        android:baselineAlignBottom="false"
        android:src="@mipmap/handcuffs" />
</LinearLayout>  

这是我最接近目标的方法,即将 ImageView 移动到此 LinearLayout 中的最右侧。这样我就可以使用 RecyclerView 创建以下示例:
在此处输入图像描述

是否可以仅通过使用 LinearLayout 来实现?Crime Title或者,除了由和组成的 LinearLayout 之外,我还需要考虑使用其他类型的布局Crime date吗?
提前致谢。

标签: androidandroid-layoutandroid-linearlayout

解决方案


无需更改布局中的任何内容

只需android:layout_weight="1"在你的秒内使用LinearLayout它就会起作用

试试这个

<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"
    android:padding="8dp">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:id="@+id/crime_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Crime Title" />

        <TextView
            android:id="@+id/crime_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Crime date" />
    </LinearLayout>

    <ImageView
        android:id="@+id/crime_police"
        android:layout_width="117dp"
        android:layout_height="50dp"
        android:layout_gravity="fill_horizontal"
        android:baselineAlignBottom="false"
        android:src="@drawable/ic_menu_send" />
</LinearLayout>

输出

在此处输入图像描述


推荐阅读