【问题标题】:How to put weight to views added dynamically on an Android layout?如何为在 Android 布局上动态添加的视图赋予权重?
【发布时间】:2020-08-25 11:10:08
【问题描述】:

我有以下布局:

<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>

但它们仍然不会增长。我如何让他们尽可能地占据整个空间?

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    你可以用这个自定义库解决它...只需将它添加到你的 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>
    

    我已经对此进行了测试,它会为您提供您想要的东西

    【讨论】:

      【解决方案2】:

      你试过了吗

      android:layout_width="match_parent"
      android:layout_height="match_parent" 
      

      对于 SeekBar?

      【讨论】:

      • 这样,它们从上到下跨越其父窗口小部件,即“标题编号编号搜索栏编号”组。他们得到了它的顶部。不是我需要的:让整个团队扩展。
      • @GuerlandoOCs,我可以请你尝试只为 layout_width="match_parent",为高度离开 "wrap_content"
      • 我得到了同样的东西
      • @GuerlandoOCs 有趣...当我测试您的代码时,我在上面的屏幕截图中得到了与您相同的结果,但是如果我对 layout_width 进行硬编码,SeekBar 将扩展... layout_width 的不同值将给出 SeekBar 的不同长度。似乎这行代码会导致问题 android:rotation="270" 如果 layout_width 是 "wrap_content" 那么 SeekBar 没有足够的空间来增长。
      猜你喜欢
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多