![](/img/trans.png)
[英]custom views in LinearLayout with weights - only first object is drawing
[英]Dynamically set the widths (weights) of Views inside a LinearLayout
我有一個包含四個不同顏色視圖的 LinearLayout。 每個彩色視圖代表鍛煉在不同心率區(藍色 -> 綠色 -> 橙色 -> 紅色)中花費的累積時間的比例。
我可以通過在 XML 的各個視圖中更改 android:layout_weight=".35" 屬性來獲得想要的效果。 設置這些值正是我想要的(當我不在代碼中覆蓋它們時)。 我想我只是沒有在代碼中正確訪問這些屬性。
我嘗試過硬編碼漂亮的圓形數字集,但它們看起來不正確。 列表中的第一個看起來比它應該的大得多。
我的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ex_history_item"
android:layout_height="160dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:theme="@style/AppTheme.CurrentStyle.HabitCard"
xmlns:android="http://schemas.android.com/apk/res/android" >
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="4dp"
android:id="@+id/cardio_bar"
android:visibility="visible"
android:weightSum="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<View
android:id="@+id/cardio_none"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".15"
android:backgroundTint="@color/cardio_zone_none"
/>
<View
android:id="@+id/cardio_fatburn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:backgroundTint="@color/cardio_zone_fatburn"
/>
<View
android:id="@+id/cardio_cardio"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:backgroundTint="@color/cardio_zone_cardio"
/>
<View
android:id="@+id/cardio_peak"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".15"
android:backgroundTint="@color/cardio_zone_peak"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/accent"
android:layout_width="35dp"
android:layout_height="match_parent"
android:background="@color/cardio"
android:backgroundTint="#00000000"
android:backgroundTintMode="add"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/ex_image"
app:layout_constraintTop_toBottomOf="@id/cardio_bar">
<TextView
android:id="@+id/bodypartLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:foregroundGravity="fill"
android:rotation="90"
android:text="Bodypart"
tools:text="Bodypart" />
</LinearLayout>
<ImageView
android:id="@+id/ex_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/accent"
app:layout_constraintTop_toBottomOf="@+id/exercise_name"
>
</ImageView>
<TextView
android:id="@+id/exercise_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercise Name"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:textSize="18sp"
app:layout_constraintLeft_toRightOf="@+id/accent"
app:layout_constraintTop_toBottomOf="@+id/cardio_bar" />
<TableLayout
android:id="@+id/cardio_table"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
app:layout_constraintBottom_toBottomOf="@+id/ex_image"
app:layout_constraintRight_toRightOf="parent"
>
<TableRow>
<TextView android:text="Duration"/>
<TextView android:id="@+id/duration" android:text="0"/>
</TableRow>
<TableRow>
<TextView android:text="Distance"/>
<TextView android:id="@+id/distance" android:text="0"/>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/weights_table"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
app:layout_constraintBottom_toBottomOf="@+id/ex_image"
app:layout_constraintRight_toRightOf="parent"
>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
我更改它們的代碼如下
private final LinearLayout cardioBar;
private final View cardioZoneNone;
private final View cardioZoneFatburn;
private final View cardioZoneCardio;
private final View cardioZonePeak;
Float total = (float)exercise.exercise.getHrTotalDuration();
holder.cardioBar.setWeightSum(total);
holder.cardioZoneNone.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrNone()));
holder.cardioZoneFatburn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrFatburn()));
holder.cardioZoneCardio.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrCardio()));
holder.cardioZonePeak.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrPeak()));
我什至嘗試硬編碼一些值來代替對象中的值。 奇怪的是,似乎將它們設置為 0.25(權重和為 1.0)看起來是正確的,但大多數其他設置的 heartZoneNone 比它應該設置的要大得多。
當我記錄這些值時,這些數字看起來很合適。
我已經讀過,只有在 XML 中使用權重屬性時,寬度才應設置為零,而不是在從代碼設置權重時。 也許這在寫的時候是真的,但是將寬度設置為零似乎可以解決我的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.