在搜索栏步骤上方添加具有不同字体大小的标签,与搜索栏拇指垂直对齐

2024-02-29

I would like to create a Seekbar, above which there will be text label on each Seekbar step, what it should look like is shown in below image this is the Expected result

这是我所做的预期结果,

<RelativeLayout
                android:id="@+id/layout_font_size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/txtFont_size_hint"
                    style="@style/textStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/Aa" />

                <LinearLayout
                    android:id="@+id/layout_seekbar_interval_holder"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtFont_size_hint"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="@dimen/margin_16dp"
                    android:orientation="horizontal"
                    android:weightSum="5">

                    <TextView
                        android:id="@+id/txtSize_14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:focusable="false"
                        android:gravity="center"
                        android:text="@string/setting_font_text"
                        android:textSize="@dimen/font_size_14" />

                    <TextView
                        android:id="@+id/txtSize_18"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:focusable="false"
                        android:gravity="center"
                        android:text="@string/setting_font_text"
                        android:textSize="@dimen/font_size_18sp" />

                    <TextView
                        android:id="@+id/txtSize_24"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:focusable="false"
                        android:gravity="center"
                        android:text="@string/setting_font_text"
                        android:textSize="@dimen/font_size_24sp" />

                    <TextView
                        android:id="@+id/textSize_30"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:focusable="false"
                        android:gravity="center"
                        android:text="@string/setting_font_text"
                        android:textSize="@dimen/font_size_30sp" />

                    <TextView
                        android:id="@+id/txtSize_36"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:focusable="false"
                        android:gravity="center"
                        android:text="@string/setting_font_text"
                        android:textSize="@dimen/font_size_36sp" />
                </LinearLayout>

                <SeekBar
                    android:id="@+id/seekBarSetting_font_size"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"                       android:layout_below="@id/layout_seekbar_interval_holder"
                    android:layout_marginLeft="@dimen/margin_16dp"
                    android:layout_marginRight="@dimen/margin_16dp"
                    android:max="4"
                    android:theme="@style/seekBarYelloStyle" />
            </RelativeLayout>

这个的输出是

布局看起来与预期类似,但是在移动搜索栏拇指时,拇指并未与布局垂直对齐TextView所以这里的问题是搜索栏拇指没有与指示搜索栏步骤的文本垂直对齐。

我还尝试通过将搜索栏宽度除以步长来计算 TextView 的确切位置(这里我们有 5 个步骤,但第一个位于第 0 个位置,所以我将总搜索栏宽度除以 4,然后相应地放置每个文本),但没有得到了预期的解决方案。

我在这里有点困惑,希望尽快找到一个简单的解决方案。

P.S:还附上动态创建的视图输出的屏幕截图以供参考


布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="android.sample.MainActivity">

<TextView
    android:id="@+id/txtFont_size_hint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sizes"/>
<LinearLayout
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:weightSum="5">

        <LinearLayout
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <TextView
        android:id="@+id/txtSize1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/setting_font_text"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/txtSize_18"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/setting_font_text"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/txtSize_24"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:gravity="center"
        android:text="@string/setting_font_text"
        android:textSize="25sp"/>

    <TextView
        android:id="@+id/textSize_30"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:gravity="center"
        android:text="@string/setting_font_text"
        android:textSize="30sp"/>

    <TextView
        android:id="@+id/txtSize_36"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:gravity="center"
        android:text="@string/setting_font_text"
        android:textSize="35sp"/>
</LinearLayout>

<SeekBar
    android:id="@+id/seekBarSetting_font_size"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="4"/>

现在我所做的是将Seekbar的宽度对齐到最后一个的中心TextView在这种情况下是txtSize_36并将 ems 设置为android:max="4"所以有五个可能的值(你可以根据需要更改它)

活动代码是:

public class MainActivity extends AppCompatActivity {
SeekBar seekBar;

private LinearLayout bar, ll;
TextView txtSize_14, txtSize_36;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBar = (SeekBar) findViewById(R.id.seekBarSetting_font_size);

    txtSize_14 = (TextView) findViewById(R.id.txtSize1);
    txtSize_36 = (TextView) findViewById(R.id.txtSize_36);
    ll = (LinearLayout) findViewById(R.id.ll);

}
    float density;
    @Override
    protected void onResume() {
    super.onResume();

    ViewTreeObserver vto = ll.getViewTreeObserver();
//****old code (may not work on all orientations)****
/*vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            int width  = ll.getMeasuredWidth();
            int height = ll.getMeasuredHeight();

            ViewGroup.LayoutParams params = seekBar.getLayoutParams();

            density = getResources().getDisplayMetrics().density;
            int newwidth = (int) (txtSize_14.getLeft() / density);
            newwidth = newwidth+ (txtSize_36.getLeft() + txtSize_36.getRight()) / 2;
            params.width = newwidth;
            seekBar.setLayoutParams(params);

        }
    });*/

    //****new code (should work on all orientations)****
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            seekBar.setPadding(15, 0, 15, 0);
            seekBar.setX(((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) - 15);

            ViewGroup.LayoutParams params = seekBar.getLayoutParams();

            int centerwidth = ((txtSize_36.getLeft() + txtSize_36.getRight()) / 2) - ((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) + 15;
            params.width = centerwidth;
            seekBar.setLayoutParams(params);
        }
    });

}

}

Here is screenshot for reference (I have clubbed for all positions of seekbar): enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在搜索栏步骤上方添加具有不同字体大小的标签,与搜索栏拇指垂直对齐 的相关文章

  • 在 Java/Android 中检查字符串是否包含 URL 的最佳方法是什么?

    在 Java Android 中检查字符串是否包含 URL 的最佳方法是什么 最好的方法是检查字符串是否包含 com net org info 其他 或者有更好的方法吗 url 输入到 Android 中的 EditText 中 它可以是粘
  • 将ForeignCollection 转换为ArrayList - ORMLite、Gson 和 Android

    如果我的解释不太清楚 我深表歉意 但如果需要 我会添加并编辑这个问题以使其清晰 我正在开发一个 Android 应用程序 它通过外部 API 接收数据并使用 ORMLite 在本地存储数据 在本地存储数据并使用 ORMLite 之前 我有一
  • Android Wear - 在没有 WearableListenerService 的情况下从 Wear 接收消息?

    我正在开发一款适用于 Android Wear 的应用程序 它可以在手机上启动活动 一切都很好 但我不想依赖手机上不断运行的后台服务来接收此消息 理想情况下 我会让 WearableListenerService 与应用程序同时启动 并保持
  • 411 需要内容长度

    我正在尝试使用 Android Apache HttpClient 执行 POST 但它返回错误 411 Content Length required 这是代码 HttpClient httpClient new DefaultHttpC
  • Android布局中如何实现Button的自动宽度

    我有几个垂直列出的按钮 我需要它们都具有相同的宽度 而且还要显示其中的所有文本 基本上我需要所有它们的宽度作为最大的宽度的包裹宽度 希望我解释得很好 现在 我已经有一种布局可以在我的 Samsung Galaxy S2 4 1 2 上运行
  • Android:如何根据视图模型实时数据属性为片段编写单元测试?

    我的片段 UI 中有一个列表视图 其元素集取决于来自视图模型 LiveData 属性的值的状态 我想为片段创建工具测试 该片段包含与该属性的值集相关的 3 个场景测试用例 但我不知道从哪里开始 我的代码应该如下所示 class MyView
  • 使用 Kotlin 协程替换 LocalBroadcastManager 以进行 Firebase 消息传递

    使用时Firebase 云消息传递 https firebase google com docs cloud messaging android client在 Android 上 通常需要通知当前Activity传入的推送通知 推荐的方法
  • 重置首选项的默认值

    我正在使用 CheckBoxPreference 作为设置屏幕 XML 是
  • 如何避免内存不足异常

    在我的应用程序中 我使用图像的 url 从网络下载图像 我有很多图像 因此我为此实现了分页技术 并按垂直顺序为每页显示 15 个图像 在这种情况下 我向上 向下滚动页面以查看图像 此时我的应用程序崩溃了 并且出现了内存不足异常 请任何人帮助
  • 如何在Android上获取角度中的按键事件?

    我们如何在 Android 上的 Angular 中获取按键事件及其值 我使用phonegap Cordova Angular JS
  • 改造添加带有令牌和 ID 的标头

    我在获取经过身份验证的用户时遇到问题 在此之前我得到了令牌和用户 ID 现在我需要使用访问令牌和 ID 从服务器获取用户 我有标题格式 https i stack imgur com OQ87Y png 现在我尝试使用拦截器添加带有用户令牌
  • 带有可点击区域的 Android 图像

    我需要建议如何在 Android 下实现以下功能 我需要一个表示类似于图形 来自离散数学 的图像 具有顶点和边缘 我可以在其中单击每个顶点或边缘并触发不同的操作 请告诉我如何实现这一目标 也许与imagebuttons 或另一种表示此功能的
  • IllegalStateException:无法更改片段的标签,以前是 android:switcher,现在是 android:switcher

    我的活动使用TabLayout ViewPager 这里的选项卡和页面的数量是动态的 具体取决于从服务器获取的数据 崩溃是通过 Crashlytics 报告的 我无法复制它 我的活动代码 Override protected void on
  • 如何创建克隆重复视图?

    在我的 Android 应用程序中 我想创建重复的ImageButton已经创建的Imagebutton 我想创造新的Imagebutton以编程方式与 XML 文件中已创建的按钮具有相同的宽度 高度 背景 图像源 边距等 简而言之 我想创
  • Android Wear Overlay 传递触摸事件

    我有一个 Android Wear 应用程序 其中我使用透明的覆盖视图来接收触摸事件 我需要能够接收屏幕上任何位置发生的所有触摸事件 并且还能够看到它们的坐标 我发现的所有研究都表明我无法接收所有触摸事件并将其传递 然而 and 都在做这样
  • 如何使用Android Invalidate()

    在我的主要活动中 我定义了两个视图和一个菜单 浏览次数 1 自定义视图游戏 2 按钮btn 菜单 1 打开项目用于打开文件 菜单布局在不同的活动中定义 基本上 当主活动启动时 它会绘制没有任何内容的自定义视图和按钮 然后我使用菜单中的 打开
  • OnSwipe 方法在 RecyclerView 中不起作用

    我正在开发一个用于播放音频文件的应用程序 创建了包含 2 个选项卡的选项卡布局 两者中都使用了片段RecyclerView两者都被使用 该片段名为LibraryFragment有这个RecycleView其物品在刷卡时必须传递给HomeFr
  • 如何在 Android 上设置 Google Drive API?

    我一直在尝试将 Google Drive 功能集成到我的应用程序中 但我无法使用任何内置功能 因此我相信我要么错过了一个步骤 要么做得不正确 我正在遵循官方的 Google 开发者指南 https developers google com
  • Android,Volley请求,响应阻塞主线程

    使用 Volley 处理较大响应时会发生一些不好的事情 String url AppHelper DOMAIN service pages profile update json this infoTextView setText getS
  • 如何在不显示父活动的情况下将一个全屏对话框片段替换为另一个全屏对话框片段?

    我有一个使用单个自定义 DialogFragment 类的活动 它的外观是数据驱动的 因此不同的调用看起来可能相当不同 它是 全屏 即 setStyle DialogFragment STYLE NO FRAME android R sty

随机推荐

  • 使用 Mongo C# 驱动程序序列化不可变值类型

    例如 我有许多不可变值类型类EmailAddress 这确保任何非空实例都是有效的 我想将这些类型的对象的序列化控制为标准字符串表示形式 email protected cdn cgi l email protection 使用 Mongo
  • C# - cc3260mt.dll 每当我加载它时都会抛出 ArithmeticException

    我有一个 WPF 应用程序 我必须加载 DLLcc3260mt dll我通过使用 LoadLibrary 来调用它 但无论出于何种原因 我都收到了 ArithmeticException 我的代码如下 public partial clas
  • 将 JSON 字符串解析为 List

    string json People FirstName Hans LastName Olo FirstName Jimmy LastName Crackedcorn var obj JObject Parse json List
  • 在 C++ 中初始化后更改引用

    class mClass public int uu 9 mClass m1 mClass m2 m1 uu 8 m2 uu 7 mClass ppp m1 ppp m2 Is it possible to change reference
  • RemoveDuplicates 函数 - 如何设置多列?

    我正在尝试使用RemoveDuplicates函数使用Excel Interop 但我对如何将列数组传递给它感到困惑 我已经知道我不能把它当作一个简单的int 数组 因为它在运行时给出异常 并且我可以传递一个整数并且它可以工作 但我希望能够
  • NSPredicate - 基于 BOOLEAN 存储值过滤值

    我有一个名为 Entry 的核心数据模型对象 在此我有一个属性 IsFavorite 我想使用 NSPredicate 来过滤 NSFetchedResultsController 的结果 目前 当执行提取时 我收到 EXC BAD ACC
  • Android 软件键盘以编程方式在数字和字母之间切换

    我有一个 EditText输入模式 文本 默认情况下 软件键盘显示为字母顺序 用户必须通过按特定键 如 123 将其切换为数字 Having textinputMode 是否可以默认显示数字键盘而不是字母键盘 我需要字母和数字 但数字的使用
  • 使用 phpmailer 的电子邮件正文中出现感叹号

    我正在使用 phpmailer 在我的网站上发送电子邮件 我的代码工作正常 但有时在电子邮件正文中随机位置包含感叹号 我的代码如下 mail gt SetFrom FROM EMAIL FROM NAME emailid of sender
  • 特殊撇号会破坏 JSON

    7671 gt Sleaford Carre s 是 result 中的一个元素 result json encode result echo result outputs 7671 null 请注意 这不是正常的撇号 单引号 或反勾号 我
  • git gc -- 积极推送到服务器?

    这好像是git gc aggressive确实清理了我的仓库 但是当我这样做时git push一切都已经是最新的了 有没有办法清理服务器 2012 I know that GitHub will run git gc periodicall
  • Android - 可以在多个帐户中发布具有相同密钥库文件的不同应用程序吗? [复制]

    这个问题在这里已经有答案了 我已上传签名apk文件与keystore在客户端Developer Account 现在 我有来自同一客户 具有不同开发人员帐户的另一个应用程序 我可以上传签名吗apk文件具有相同的keystore文件或者我应该
  • Fluent nHibernate 和 asp.net MVC 入门

    我考虑使用 Fluent nhibernate 通过我的 asp net mvc web 应用程序进行数据访问 任何描述如何使用 asp net mvc web 应用程序设置 Fluent nHibernate 的好文章 This seri
  • Laravel 迁移 - 完整性约束违规:1452 无法添加或更新子行:外键约束失败

    我正在尝试对表运行迁移inventories我通过这次迁移创建的 Schema create inventories function Blueprint table table gt increments id table gt inte
  • 使用正则表达式前瞻,egrep

    如果您的文件包含 apples are good apple cider is also good 为什么会egrep apples app file无法接听任何线路 在 MAC 上使用egrep 2 5 1 扩展正则表达式不具有积极的前瞻
  • 实体框架 4.1 InverseProperty 属性

    只是想了解更多RelatedTo属性 我发现它已被替换为ForeignKey and InversePropertyEF 4 1 RC 中的属性 有谁知道有关此属性变得有用的场景的任何有用资源 我应该在导航属性上使用此属性吗 例子 publ
  • 使用 Rails 测试 PayPal

    我需要测试新的 PayPal 与我的应用程序的集成 我在开发环境中使用 PayPal 沙箱 为了测试整个界面 我发现ngrok https ngrok com 可能会解决来自 PayPal 的消息问题 我使用安装了 ngrokapt get
  • 如何在 IBM DB2 中的导出结果中包含列标题

    我试图使用命令从 DB2 中的表中导出数据 我想导出到 csv 文件中 但是列标题不存在 还需要列标题 那么我可以在 sql 查询中添加什么也获取列标题 这在最新的 2019 Db2 11 5 版本和 Db2 Warehouse 本地和云上
  • 电池使用情况 - iOS

    我想在应用程序的状态栏上显示应用程序的电池使用情况 它使用 GPS 因此 GPS 信号和应用程序的电池使用详细信息对于用户了解电池消耗情况很有用 有没有办法获取设备的电池使用情况 并仅显示消耗的电池量my app独自的 在仪器上 它显示随时
  • 带有目标目录的符号链接二进制文件

    我有一个二进制文件 它加载具有相对路径的资源 我想在 usr bin 中放置一个链接 我努力了ln s game directory game usr bin game 它启动了我的二进制文件 但无法加载我的资产 因为它显然找不到文件夹 a
  • 在搜索栏步骤上方添加具有不同字体大小的标签,与搜索栏拇指垂直对齐

    I would like to create a Seekbar above which there will be text label on each Seekbar step what it should look like is s