我想在同一行中从左到右添加多个带有文本视图的按钮,通常会将方向设置为水平,但我需要垂直才能将文本视图“现金应用程序”放置在我所在的图像下需要它我怎样才能做到这一点?我听说过一些有关容器布局的信息,但不知道如何做到这一点,任何帮助将不胜感激。
首先,您可以将“按钮”包装在另一个 XML 布局中,例如,我们将其命名为
custom_view_button.xml
。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageViewCashApp"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/cashapp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cash app"
/>
</LinearLayout>
然后在您的activity_layout.xml中,将其包含如下。记下
weightSum
和 layout_weight
值,以便宽度相等。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<include
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
layout="@layout/custom_view_button"
/>
<include
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
layout="@layout/custom_view_button"
/>
</LinearLayout>