在 GridLayout 中使 android 水平 LinearLayout 适合列

Make android horizontal LinearLayout fit column in GridLayout

我正在尝试制作一个视图,其中包含从左到右顺序的一些元素和一个按钮,该按钮以固定大小附加到右上角。我当前的布局是:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_column="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/Image1"
            android:layout_height="40dp"
            android:layout_width="40dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginBottom="16dp"
            android:src="@drawable/image_src"
            android:visibility="gone" />
        <TextView
            android:id="@+id/Text1"
            android:text="SampleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="24dp"
            android:textSize="16sp"
            android:gravity="center" />
    </LinearLayout>
    <ImageButton
        android:id="@+id/Button1"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="right"
        android:layout_column="1"
        android:src="@drawable/imageBtn"
        style="?android:attr/borderlessButtonStyle" />
</GridLayout>

当文本很短时,一切似乎都很好

但问题是文本可能会很长,而且会与按钮重叠。

如何使按钮始终可见并且文本完全环绕在按钮的左上角?

您可以使用 RelativeLayout 实现此目的。在您的 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="wrap_content">

    <ImageButton
        android:id="@+id/Button1"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:layout_marginTop="24dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/Button1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/Image1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:src="@mipmap/ic_launcher"
            android:visibility="visible" />

        <TextView
            android:id="@+id/Text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="24dp"
            android:text="SampleText sdfsd sd sdf sdfs  sdfsd fsf fsdf sd s dsfs sfs sfs sdf s fsdf sdf fsdf  sdf sf sd fsdf sfsdf sfsd ffs sdf sdf sfsdf sfs sdfs sfsdfs sfs sf sfs fsfsf sf ssf "
            android:textSize="16sp" />
    </LinearLayout>
</RelativeLayout>