繁体   English   中英

在 LinearLayout 中动态设置视图的宽度(权重)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM