材质滑块错误的拖动标签显示

时间:2020-04-23 19:06:53

标签: android slider rangeslider

如果我使用here范围滑块中的材质滑块示例,则拖动看起来像这样: enter image description here 但是当我在应用程序中使用它时,当我拖动滑块时,我只会看到一个滑块标签: enter image description here

我该如何解决?我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"

    android:focusable="true"

    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:paddingStart="15dp"
    android:paddingEnd="15dp"
    tools:context=".presentation.sub_search.view.SubSearchFragment">


    <com.google.android.material.slider.Slider
        android:id="@+id/prices_slider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:layout_marginTop="40dp"
        android:stepSize=".1"
        android:valueFrom="0"

        android:valueTo="100" />
</LinearLayout>

Slider pricesSlider = view.findViewById(R.id.prices_slider);
pricesSlider.setValues(10f, 70f);

UPD:

我只有片段内部有此问题。如果我在活动中使用相同的代码,就不会有问题

2 个答案:

答案 0 :(得分:0)

问题出在片段容器中。将容器更改为CoordinatorLayout后,问题得以解决。在其他地方,我将ViewPager替换为ViewPager2后已解决的问题

答案 1 :(得分:0)

在此代码中,例如

@ cren90

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new SliderFragment()).commit();
    }
}

slider_fragment.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.slider.Slider
            android:id="@+id/prices_slider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stepSize="1"
            android:valueFrom="0"
            android:valueTo="100"
            />
</RelativeLayout>

您需要在main_activity.xml中用androidx.coordinatorlayout.widget.CoordinatorLayout替换FrameLayout