java - 在 LinearLayout 中将左侧图像移至右侧

标签 java android android-linearlayout android-relativelayout

我想把左边的图像移到右边,但我做不到。 我知道我可以使用 Layout_Direction="rtl" 但我不想使用它。在某些手机上,布局已损坏

enter image description here

这是我的layout.xml

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:minHeight="?attr/actionBarSize"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/icon_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/spacing_large"
            android:layout_marginTop="@dimen/spacing_middle"
            android:layout_marginRight="@dimen/spacing_large"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/icon_front"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fillAfter="false"
                android:fillEnabled="false">

                <ImageView
                    android:id="@+id/icon_profile"
                    android:layout_width="40dp"
                    android:layout_height="40dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </RelativeLayout>

        </RelativeLayout>

        <View
            android:layout_width="@dimen/spacing_medium"
            android:layout_height="0dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/message_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:orientation="vertical"
                android:paddingTop="@dimen/spacing_middle"
                android:paddingBottom="@dimen/spacing_middle">

                <TextView
                    android:id="@+id/from"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/spacing_middle"
                    android:layout_marginRight="@dimen/spacing_middle"
                    android:gravity="right"
                    android:maxLines="1"
                    android:text="People Name"
                    />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/spacing_medium"
                        android:layout_marginEnd="@dimen/spacing_middle"
                        android:layout_marginRight="@dimen/spacing_middle"
                        android:gravity="right"
                        android:maxLines="1"
                        android:text="some new text for new items"
android:textAppearance="@style/TextAppearance.AppCompat.Small"/>
                </LinearLayout>
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey_10" />

        </LinearLayout>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

那么如何将图像放在右侧呢?另外,重力不起作用我尝试使用重力但没有将图像向右移动

最佳答案

ImageView 位于 RelativeLayout 内部,因此您需要添加以下属性:

android:layout_alignParentEnd="true"

或者如果您不需要 RTL 支持:

android:layout_alignParentRight="true"

xml 将是:

<ImageView
    android:id="@+id/icon_profile"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentEnd="true"/>

关于java - 在 LinearLayout 中将左侧图像移至右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55985910/

相关文章:

java - 如何在 Hibernate 中序列化集合属性?

java - Mercurial - 文件未在服务器上显示(ls 命令)

Android:为什么屏幕上没有任何显示?

android - 意外的向后兼容性或为什么我的应用程序没有崩溃?

Android:将标题 View 添加到 ExpandableListView 时出现 ClassCastException

android - 使用其中的 ImageButton 捕获 LinearLayout onClick 事件

java - 如何从我的对话框 (Android) 访问我的 GridView 实例?

java - 测试覆盖率在 IntelliJ IDEA 中不起作用

java - Android单列Gridview

java - 每次按键时更新 TextView