【问题标题】:Customize seeekbar endpoints自定义 seeekbar 端点
【发布时间】:2018-07-30 12:27:21
【问题描述】:

我想更新搜索栏 UI,使搜索栏的两个端点都有一条垂直线。我通过在 android:progressDrawable 属性中设置自定义可绘制图像(在端点处带有垂直线的水平线)来尝试此操作,但该搜索栏不可见(只有拇指可见)。我还尝试在搜索栏的左侧和右侧创建自定义视图,但垂直线不会保持在不同设备中的确切位置。此外,由于搜索栏具有默认的左右填充,我需要提供边距以在搜索栏端点处准确显示垂直线,这对于不同的设备可能会有所不同。

实现此要求的理想方法是什么?

<SeekBar 
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="@dimen/dp1" 
android:paddingLeft="0px" 
android:paddingRight="0px" 
android:progressDrawable="@color/white"/>

<View
android:layout_width="@dimen/dp1"
android:layout_height="@dimen/dp10"
android:layout_alignLeft="@+id/seekBar"
android:layout_alignTop="@+id/seekBar"
android:layout_marginLeft="@dimen/dp16"
android:bawrckground="@color/white"
android:id="@+id/view" />
<View
android:layout_width="@dimen/dp1"
android:layout_height="@dimen/dp10"
android:layout_alignRight="@+id/seekBar"
android:layout_alignTop="@+id/seekBar"
android:layout_marginRight="@dimen/dp16"
android:background="@color/white”/>

【问题讨论】:

  • 好的。你能展示任何原型吗??
  • @IntelliJAmiya 目前我无法共享图像。但是 Seekbar 应该看起来像一条带有垂直端点的水平线。垂直线应该与水平线成直角。
  • 这是一个 xml 问题。
  • @IntelliJAmiya 垂直线应该正好在 seekbar 轨道端点的位置。问题是 seekbar 有一些默认填充,因此这些线在轨道端点处不完全可见。
  • @IntelliJAmiya 另外垂直线应该与水平线成直角。它不应该低于水平线。

标签: android android-layout seekbar android-seekbar


【解决方案1】:

我猜你已经回答了你的问题。正如您所说,差异仅在于左右填充,那么您可以通过简单地更改视图的边距来实现这一点,如下所示。 假设这是您的 XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <View
        android:id="@+id/view1"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/seekBar"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/seekBar"
        android:background="@android:color/black" />

    <View
        android:id="@+id/view2"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/seekBar"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/seekBar"
        android:background="@android:color/black" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:maxHeight="1dp"
        android:progressDrawable="@android:color/black" />
</RelativeLayout>

然后只需在您的 java 文件中添加以下行:

SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
((ViewGroup.MarginLayoutParams) findViewById(R.id.view1).getLayoutParams()).leftMargin = seekBar.getPaddingLeft();
((ViewGroup.MarginLayoutParams) findViewById(R.id.view2).getLayoutParams()).rightMargin = seekBar.getPaddingRight();

这绝对不是最好的解决方案,但它非常简单。

这里有几张图片说明它的样子

【讨论】:

    【解决方案2】:

    你看起来像这样吗?我使用了布局权重,端点为 0.05,搜索栏为 0.90 (%)……也给了拇指。您可以根据自己的要求进行更改。

    <LinearLayout
        android:id="@+id/ll_wizard_bar"
        style="@style/Widget.AppCompat.ButtonBar"
        android:layout_width="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_height="30dp"
        android:layout_margin="18dp"
        android:orientation="horizontal">
        <View
            android:layout_width="0dp"
            android:layout_weight=".05"
            android:layout_height="30dp"
            android:background="#FF0000FF" />
    
        <SeekBar
            android:id="@+id/my_seekbar"
            android:layout_width="0dp"
            android:layout_weight=".90"
            android:layout_height="wrap_content"
            android:maxHeight="15dip"
            android:minHeight="15dip"
            android:thumb="@android:drawable/star_big_off"
            android:progressDrawable="@drawable/oval_shape"
             android:layout_below="@+id/iv_consumericon"
            android:indeterminate="false"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            />
    
        <View
            android:layout_width="0dp"
            android:layout_weight=".05"
            android:layout_height="30dp"
            android:background="#FF0000FF" />
    </LinearLayout>
    

    去除边缘

    要将搜索栏附加到您的垂直线.. 以编程方式切断填充。 my_seekbar.setPadding(0, 0, 0, 0);:礼貌 https://stackoverflow.com/a/33475281/705428

    【讨论】:

    • 垂直线应该正好在seekbar可见端点的位置。问题是seekbar有一些默认填充,所以这些线在端点处不完全可见。
    • 在您的情况下,黑白垂直线和搜索栏端点也存在一些间隙。
    • 这是您原始问题中未提及的新要求....但是,Android会根据重击图像大小调整搜索栏..以便可以看到完整的拇指。如果您删除我的自定义拇指并采用android支持的默认拇指...它应该可以完成您的工作...。另一种选择是使用相对布局并将端点视图重叠在图像的边缘。
    • 我认为从问题中可以理解-“此外,由于 seekbar 具有默认的左右填充,我需要提供边距以在 seekbar 端点处精确显示垂直线,这对于不同的设备可能会有所不同。”
    • 我现在只使用默认拇指
    【解决方案3】:
    • Seekbar 具有默认的左右填充

    解决方案

    你可以使用Programmatic方式。在这里,调用setPadding

    视图可能会增加显示滚动条所需的空间, 取决于滚动条的样式和可见性。所以价值观 从 getPaddingLeft()、getPaddingTop()、getPaddingRight() 和 getPaddingBottom() 可能与此调用中设置的值不同。

    演示 XML

     <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent"
        tools:context=".MainActivity">
    
        <View
            android:id="@+id/view1"
            android:layout_width="10dp"
            android:layout_height="50dp"
            android:background="#54d66a"
             />
    
        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:progress="0"
             android:max="100"
            android:layout_toRightOf="@+id/view1"/>
    
        <View
            android:layout_width="10dp"
            android:layout_height="50dp"
            android:background="#000000"
            android:layout_toRightOf="@+id/seekBar"
            />
    </RelativeLayout>
    

    Java 类

    oncreate()

    SeekBar SeekBarOb=(SeekBar)findViewById(R.id.seekBar);
    SeekBarOb.setPadding(20,0,0,0);
    

    不好的方法

    使用硬编码值。调用 DisplayMetrics

    描述有关显示的一般信息的结构,例如 它的大小、密度和字体缩放。

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    
            int DeviceTotalWidth = metrics.widthPixels;
            int DeviceTotalHeight = metrics.heightPixels;
    

    现在响应式

     SeekBarOb.setPadding(DeviceTotalWidth*2/100 ,0,0,0); // add your value
    

    终于

    仅供参考

    不要更改演示 XML,将其添加到您的 java 类中

     DisplayMetrics metrics = getResources().getDisplayMetrics();
    
        int DeviceTotalWidth = metrics.widthPixels;
        int DeviceTotalHeight = metrics.heightPixels;
    
        SeekBar SeekBarOb=(SeekBar)findViewById(R.id.seekBar);
        SeekBarOb.setPadding((int) (DeviceTotalWidth*.85/100),0, (int) (DeviceTotalWidth*2.10/100),0);
    

    【讨论】:

    • 但这些不是可聚焦的小部件。
    • 当我将填充设置为 0 时,如果我将它拖到端点,Seekbar 拇指会被切断/剪掉。但是在屏幕截图中,你发布了它的显示效果很好。你怎么能做到这一点?如果您将填充设置为 0,拇指不会在您的情况下被切断吗?
    • @AndroidLearner 你在那里使用了#DisplayMetrics 规则吗?
    • 不,我只是使用 setPadding 将填充添加到 0
    • 基本上我想要的是垂直线应该完全附加到搜索栏轨道端点,它们之间没有间隙。另外我希望搜索栏拇指在拖动到端点时不应该被切断。你正在添加填充搜索栏的左侧和右侧,这将使搜索栏拇指在拖动到端点时完全可见,但会在垂直线和搜索栏轨道之间添加空间。如果我们可以通过某种方式使搜索栏拇指保持在轨道内,即使它被拖动到端点并将填充设置为 0,我想要的也可以实现。
    猜你喜欢
    • 2018-09-02
    • 2018-08-19
    • 1970-01-01
    • 2019-10-03
    • 2012-09-26
    • 2021-04-24
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多