首页 > 解决方案 > Android 三项并排

问题描述

我正在尝试为我的 recyclerview 创建一个布局,其中三个项目水平相邻。

我希望通过 LinearLayout 来实现这一点,但最后一项(ImageButton)不会出现。我也一直在尝试权重组合。

有没有好的方法来实现这一目标?以下是我当前的布局文件。

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal">
<TextView
    android:id="@+id/textView_option"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/option"
    android:layout_margin="15dp"
    android:textSize="20dp"
    />

<EditText
    android:id="@+id/editText_option"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:lines="1" />

<ImageButton
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:src="@drawable/icon_minus"
    android:scaleType="fitCenter"
    android:background="@null"
    android:layout_weight="2"
    />
 </LinearLayout>

标签: android

解决方案


尝试用下面的代码替换它

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="3"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView_option"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="option"
            android:layout_weight="1"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/editText_option"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_weight="1"
            android:lines="1" />

        <ImageButton
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_weight="2"
            android:background="@null"
            android:scaleType="fitCenter"
            android:src="@drawable/icon_minus" />
    </LinearLayout>

推荐阅读