【问题标题】:UI Compatibility Issues Between Different API Versions不同 API 版本之间的 UI 兼容性问题
【发布时间】:2017-07-21 22:01:54
【问题描述】:

我正在开发一个使用 Navigation Drawer(我从 Android Studio 的 Activity 模板生成)的应用,该应用在内部使用 fragments。导航抽屉嵌套在我的MainActivity 中。我还在工具栏中添加了一个菜单,其中还有两个选项;过滤器和设置(也从 AS 模板生成的设置)。我在 API 19 和 24 之间测试应用时遇到了问题。

我的 LoginActivity 使用 drawableLeft 属性和 drawableTint(我知道它仅适用于 API23 及更高版本)。如何让 drawableLeft 图标在旧版本上显示为白色?

我的第二个更重要的问题涉及Toolbar 及其兼容性。在 API 24(使用 Nexus 5X 模拟)中,Toolbar 在状态栏下方对齐良好,而在 API 19 中,Toolbar 位于状态栏下方,状态栏也有不匹配的颜色。现在,我知道工具栏是 Material Design 的新增功能,因此受到 API 23+ 的支持(我认为),但是解决此问题的正确方法是什么?

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.example.michael.whatsupldn.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/london_skyline_dark"
        android:layout_alignParentTop="true"
        android:id="@+id/imageView"
        android:contentDescription="@string/london_skyline"/>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:fitsSystemWindows="true"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

任何帮助将不胜感激!

附:如果需要任何代码来帮助分析,请尽管询问。

编辑

我在我的 FilterActivity 中也遇到了同样的Toolbar 问题...

更新

fitsSystemWindows 应用到AppBarLayoutCoordinatorLayout 后,这是以下结果,这并没有解决我的问题。它实际上使情况变得更糟,因为它破坏了 API 24 中的输出。

【问题讨论】:

  • 对于第 1 期:您是在设置可绘制对象的颜色,例如 @color/white 还是 #ffffff?
  • @UsmanRana 我是这样设置的:android:drawableTint="#FAFAFA"
  • 你在为这个drawable使用矢量资源吗?
  • @ArnabKundu 是的,我是

标签: android android-layout android-toolbar android-resources android-4.4-kitkat


【解决方案1】:

我觉得你的矢量代码是这样的.....

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="@color/colorWhite"
    android:pathData="M12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 
    0.9,2 2,2zM18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 
    -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 
    -2,-2zM8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1s3.1,1.39 
     3.1,3.1v2L8.9,8L8.9,6zM18,20L6,20L6,10h12v10z"/>
 </vector>

修改这行代码

     android:fillColor="@color/colorWhite"

到.....

     android:fillColor="#FFFFFF"

如果我没记错的话,这就是你的解决方案。

【讨论】:

  • 是的,这行得通 - 非常感谢,很好的回答!您是否对我提到的第二个问题有所改变?
  • 你可以通过这个与buttonTint相关的链接。但是对于我使用的复选框。不确定它是否适合您。
【解决方案2】:

如何让 drawableLeft 图标在旧版本上显示为白色?

您可以使用DrawableCompat:

Drawable drawable = getResources().getDrawable(R.drawable.some_drawable);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), getResources().getColor(android.R.color.white));

Source

解决此问题的正确方法是什么(工具栏对齐)?

android:fitsSystemWindows="true" 应用于Toolbar 及其父母。

【讨论】:

  • 好的,我现在已经包含在工具栏中了,但是它的父级是什么?我已经为 app_bar 包含了我的 XML 代码...
  • fitsSystemWindows 应用到AppBarLayoutCoordinatorLayout
  • 请看我的帖子,我已经用输出更新了它。它对我不起作用。
  • @Programmer101,应用this answer中指定的样式。
  • 我已经查看了答案,但是,这似乎对我没有帮助,因为它专注于透明度,而我正在寻找的是 ActionBar / @987654332 的帮助@布局兼容性。
【解决方案3】:

您可以以编程方式设置颜色,它会为您工作:

// 0=left, 1=top , 2=right , 3=bottom
    editText.getCompoundDrawables[0].mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); 

【讨论】:

    【解决方案4】:

    感谢 Arnab Kuntu 解决了问题 #1。

    问题 #2 幸运的是,我在玩 styles 中的主题时为自己找到了解决方案。我的AppTheme&lt;item name="android:windowTranslucentStatus"&gt;true&lt;/item&gt; 中嵌套了以下声明,使Status bar 透明,这使模拟器认为没有基础来支持下面的Action Bar / Toolbar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多