【问题标题】:Android mancj.MaterialSearchBar: Using reflection to override and change some API layout and widgets, font-family, boldness and paddingAndroid mancj.MaterialSearchBar:使用反射覆盖和更改一些 API 布局和小部件、字体系列、粗体和填充
【发布时间】:2019-03-11 22:40:15
【问题描述】:

我正在使用https://github.com/mancj/MaterialSearchBar 设置带有菜单汉堡的搜索栏。 我无法更改 API。

这是我得到的:

问题

如何...:

  1. 减少文本的左边距?

  2. 改变后者的font-family?

  3. 最后,禁用粗体?

注意:这些问题由其他一些人在下一节中完成,根据文档

根据文档

mt_aBCD 是允许我们自定义搜索栏的 XML 属性。但是,上面的那些都不存在。他们的 Java 等价物(setABCD 两者都不是)。

顺便提一下,这个 API 的 Github 活动并不多。我不能用另一个替换后者。你会给我什么解决方案?

  1. 我是否应该更改此 API 中的某些内容(是否经过授权且合法)? (怎么做?)
  2. 我可以覆盖样式吗?等等(如何?)
  3. 其他?

我的实现

我编辑了我的构建文件,然后我只是在我的 XML 布局中添加了搜索栏:

    <com.mancj.materialsearchbar.MaterialSearchBar
    android:id="@+id/material_search_bar"
    style="@style/MaterialSearchBarLight"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="15dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="15dp"
    app:layout_constraintBottom_toBottomOf="@+id/toolbar"
    app:layout_constraintEnd_toEndOf="@+id/toolbar"
    app:layout_constraintStart_toStartOf="@+id/toolbar"
    app:layout_constraintTop_toTopOf="@+id/toolbar"
    app:mt_maxSuggestionsCount="10"
    app:mt_navIconEnabled="true"
    app:mt_placeholder="@string/search_placeholder" />

【问题讨论】:

    标签: android android-widget android-xml android-library


    【解决方案1】:

    第一刻:

    这个库不支持字体家族。此问题存在于github 块“ISSUE


    第二个时刻:

    为您的文本填充不支持喜欢和“禁用粗体”


    但是你可以用反射解决你的问题

    这不是最好的解决方案,但它确实有效

        searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
        try {
            final Field placeHolder = searchBar.getClass().getDeclaredField("placeHolder");
            placeHolder.setAccessible(true);
            final TextView textView = (TextView) placeHolder.get(searchBar);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    
            // fix trouble number 1 paddingLeft
            textView.setPadding(0, 0, 0, 0);
            final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
            layoutParams.setMargins(0,0,0,0);
    
            //fix trouble number 2 font-family and number 3 Finally, disable boldness?
            Typeface typeface = Typeface.createFromAsset(getAssets(), getString(R.string.roboto_medium));
            //<string name="roboto_medium">fonts/Roboto-Medium.ttf</string>
            textView.setTypeface(typeface);
    
            textView.setText("Hello World!");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    

    汉堡菜单的大小为 48dp,它不是边距(DeclaredField = navIcon)


    最后一个变种

    在你的项目中复制这个库并编辑你想要的所有内容。
    你可以这样做,因为这个库使用了license MIT 最重要的是指明原作者

    【讨论】:

    • @JarsOfJam-Scheduler 我修正了我的答案,看看吧。
    • NB:Typeface typeface = ResourcesCompat.getFont(Objects.requireNonNull(getContext()), R.font.XYZ); 是您致电 Typeface typeface = Typeface.createFromAsset(Objects.requireNonNull(getActivity()).getAssets(), "fonts/newscycle_regular.ttf"); 的替代方案:-)。参照。 stackoverflow.com/questions/48688026/…
    • 是的,你可以或so
    • 我认为是的,我答案中的最后一张图片显示了所有字段和图标的区域。
    • 不是宽度问题。问题是由于句子太长,被这个 API 严重切割:-)。再次感谢您!
    【解决方案2】:

    我用这个解决了同样的问题:

    Typeface typeface = ResourcesCompat.getFont(Objects.requireNonNull(getContext()), R.font.XXX);
    placeHolder = findViewById(R.id.mt_placeholder);
    placeHolder.setTypeface(typeface);
    

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 1970-01-01
      • 2011-05-26
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      相关资源
      最近更新 更多