這是到目前為止我為輪播所做的每張卡片撰寫的代碼。此外,不確定是使用 ImageButton 還是 Button,因為我正在嘗試為用戶按下“-”/“ ”按鈕。
<androidx.appcompat.widget.LinearLayoutCompat 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:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="22dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- ImageView for images placed above the text-->
<ImageView
android:id="@ id/imageIv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop" />
<!-- TextView is right below the image-->
<TextView
android:id="@ id/flavorTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Flavor 1"
android:textSize="15sp" />
<!-- Trying to place the button on the same row as the TextView-->
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>

uj5u.com熱心網友回復:
用 ConstraintLayout 替換你的內部 LinearLayout,這樣你就可以根據你定義的約束來定位視圖。就像下面的例子:
<androidx.appcompat.widget.LinearLayoutCompat 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:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="22dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- ImageView for images placed above the text-->
<ImageView
android:id="@ id/imageIv"
android:layout_width="0dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- TextView is right below the image-->
<TextView
android:id="@ id/flavorTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Flavor 1"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/imageIv"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- Trying to place the button on the same row as the TextView-->
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
app:layout_constraintTop_toBottomOf="@id/imageIv"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/flavorTv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
uj5u.com熱心網友回復:
選項1:
將 textview 和 image 包裝在線性布局中:
<!-- TextView is right below the image-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@ id/flavorTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Flavor 1"
android:textSize="15sp" />
<!-- Trying to place the button on the same row as the TextView-->
<ImageButton
android:layout_gravity="bottom"
android:layout_marginBottom="5dp"
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
根據您的要求調整邊距,填充。
選項 2:
將可繪制與文本視圖一起使用:
<TextView
android:drawableEnd="@drawable/cald"
android:id="@ id/flavorTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Flavor 1"
android:textSize="15sp" />
洗掉 ImageButton,使用圖示并設定 textview.setOnviewClickListener 以添加用于影像導航的代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/484976.html
下一篇:將C#物件轉換為XML持久性格式
