Material slider错误拖动标签显示
Material slider wrong drag label show
如果我使用 here 范围滑块中的 material 滑块示例,当拖动看起来像这样时:
但是当我在我的应用程序中使用它时,当我拖动滑块时,我只看到一个滑块标签:
我该如何解决?我的代码:
<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);
更新:
我只在片段内部有这个问题。如果我在 activity 中使用相同的代码,我没有问题
问题出在片段容器中。在我将容器更改为 CoordinatorLayout 问题后修复。在我将 ViewPager 替换为 ViewPager2
后,其他地方的问题得到解决
@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
中将 FrameLayout 替换为 androidx.coordinatorlayout.widget.CoordinatorLayout
如果我使用 here 范围滑块中的 material 滑块示例,当拖动看起来像这样时:
我该如何解决?我的代码:
<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);
更新:
我只在片段内部有这个问题。如果我在 activity 中使用相同的代码,我没有问题
问题出在片段容器中。在我将容器更改为 CoordinatorLayout 问题后修复。在我将 ViewPager 替换为 ViewPager2
后,其他地方的问题得到解决@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
中将 FrameLayout 替换为 androidx.coordinatorlayout.widget.CoordinatorLayout