我有以下布局:
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cat_card_list_item_card"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:minHeight="118dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/effectContainer"
android:orientation="vertical"/>
</com.google.android.material.card.MaterialCardView>
它会随着对象动态添加,如下所示:
Tremolo + 按钮是一个对象,其余是 2 个对象,每个对象都有一个垂直搜索栏。我希望第一个对象具有高度 wrap_content
,看起来确实如此,其余对象尽可能垂直扩展,以便搜索栏增长。我将他们的高度设为“wrap_content”:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/paramWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/paramLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1" />
<TextView
android:id="@+id/curLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle2" />
<TextView
android:id="@+id/minLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="10"
android:rotation="270" />
<TextView
android:id="@+id/maxLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" />
</LinearLayout>
但它们仍然不会生长。我如何让它们尽可能占据整个空间?
最佳答案
你可以用这个自定义库来解决它...只需将它添加到你的 build.gradle
implementation 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:1.0.0'
你的 XML 应该是这样的:
<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="10">
<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar
android:id="@+id/mySeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:splitTrack="false"
app:seekBarRotation="CW90" /> <!-- Rotation: CW90 or CW270 -->
</com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper>
我已经测试过了,它会给你你想要的东西
关于java - 如何对在 Android 布局上动态添加的 View 施加权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61703804/