我有一個線性布局,列中有六個水平按鈕的串列。每個 Button 都定義為 a,AppCompactTextView因為我需要在其上放置兩個影像,如下圖所示:

我需要做的是將drawableStartCompat影像(左側的那個)與 AppCompactTextView 分開,而不改變它的位置。我想讓它與按鈕分開。怎么辦?我想用視圖中的按鈕包裝它,但我不知道如何管理它。這是串列的六個元素之一的代碼:
<androidx.appcompat.widget.AppCompatTextView
android:id="@ id/view_top_up_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="@string/placeholder"
android:textColor="@color/black"
app:drawableEndCompat="@drawable/ic_arrow_right_small_black"
app:drawableStartCompat="@drawable/ic_menu_charge" />
uj5u.com熱心網友回復:
您可以將Imageview&包裝TextView到您喜歡的任何視圖組中并創建一個像這樣的視圖,這使您可以靈活地為視圖維護邊距位置等:
<?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="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@ id/leftImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="@drawable/ic_android" />
<TextView
android:id="@ id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_weight="1"
android:padding="8dp"
android:text="Boost"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@ id/rightImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:src="@drawable/ic_arrow_right" />
</LinearLayout>
</LinearLayout>
輸出:

您甚至可以繼續并從您的布局中創建自定義/復合視圖。
uj5u.com熱心網友回復:
從 Horizo??ntal 創建自定義按鈕LinearLayout,其中包含ImageView,Button也許還有另一個ImageView用于右側的影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452384.html
下一篇:在特定(最初為空)路徑中插入元素
