android 布局 横向文本框带图标文本框wrap_content声明但是当图标顶到屏幕后文本框不在变大
要实现横向文本框带图标,并且当图标顶到屏幕后,文本框自动变大,可以使用LinearLayout和Weight属性来布局。
首先,创建一个横向的LinearLayout布局,设置其方向为水平:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 图标 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
<!-- 文本框 -->
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" />
</LinearLayout>
在上述代码中,图标使用ImageView来表示,文本框使用EditText来表示。LinearLayout的方向设置为水平,图标和文本框按照水平方向依次排列。
注意文本框的宽度设置为0dp,然后使用android:layout_weight="1"来设置权重。这样,当图标顶到屏幕后,文本框会自动拉伸以填充剩余空间。
请确保将@drawable/icon替换为您自己的图标资源
原文地址: http://www.cveoy.top/t/topic/iXbD 著作权归作者所有。请勿转载和采集!