在工具栏中占据剩余空间的 Seekbar

4 浏览
0 Comments

在工具栏中占据剩余空间的 Seekbar

我正在尝试创建一个工具栏,其中左边固定文本,右边固定文本,中间是一个占据剩余空间的SeekBar。

我已经尝试了一些布局,但没有成功。

这是我的代码:

android:id="@+id/toolbar_bottom"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:minHeight="?android:attr/actionBarSize"

android:background="?attr/colorPrimary"

android:layout_alignParentBottom="true"

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_centerVertical="true"

android:text="左边文本"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:layout_toRightOf="@+id/left"

android:layout_toLeftOf="@+id/right"

android:max="999"

android:progress="100"

android:paddingLeft="15dp"

android:paddingRight="15dp"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:text="右边文本"/>

似乎无法让SeekBar占据剩余空间。

0
0 Comments

问题的原因是在Toolbar中添加了一个LinearLayout,并且SeekBar的宽度设置为0dp并使用了layout_weight属性来占据剩余空间。由于Toolbar本身是一个固定高度的View,它的高度由属性android:minHeight指定,而LinearLayout的高度设置为match_parent,这导致SeekBar的高度无法正确计算。

解决方法是将LinearLayout的高度设置为wrap_content,这样它的高度将根据子View的实际高度来计算,同时也可以将SeekBar的高度设置为match_parent。这样SeekBar将占据LinearLayout剩余的空间,并且Toolbar的高度将由子View的高度来确定。

修改后的代码如下:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="?attr/colorPrimary"
    android:minHeight="?android:attr/actionBarSize"
    app:theme="/ThemeOverlay.AppCompat.Dark.ActionBar">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left Text"/>
        <SeekBar
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:max="999"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:progress="999"/>
        <TextView
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Right Text"/>
    </LinearLayout>
</android.support.v7.widget.Toolbar>

0