【发布时间】:2019-06-24 04:02:28
【问题描述】:
我有一个布局,其中只有一部分依赖于 api 级别(它使用向量),我需要针对 api-19 以不同的方式处理它。我更改了布局以处理使用app:srcCompat:"@drawable/left_arrow_vector" 的布局,这可以使 19 不会崩溃,但是当我在其他 api 级别上测试它时,它仍然显示与 api-19 相同的布局(存在尺寸问题,这使得它很容易看。我会在弄清楚这部分后解决这个问题。
res/layout-v19:
<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:id="@id/calTitleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ImageView
android:id="@id/prevMonth"
app:srcCompat="@drawable/left_arrow_vector"
android:contentDescription="@string/prev_month"
android:layout_height="24dp"
android:layout_width="24dp" />
<TextView
android:id="@id/calMonth"
style="@style/CalendarTitle"
android:layout_toStartOf="@id/nextMonth"
android:layout_toEndOf="@id/prevMonth"
tools:text="JANUARY 2019" />
<ImageView
android:id="@id/nextMonth"
android:layout_alignParentRight="true"
app:srcCompat="@drawable/right_arrow_vector"
android:contentDescription="@string/next_month"
android:layout_height="24dp"
android:layout_width="24dp" />
</RelativeLayout>
分辨率/布局:
<?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:id="@id/calTitleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ImageButton
android:id="@id/prevMonth"
style="@style/CalendarLeftArrowButton"
android:contentDescription="@string/prev_month" />
<TextView
android:id="@id/calMonth"
style="@style/CalendarTitle"
android:layout_toStartOf="@id/nextMonth"
android:layout_toEndOf="@id/prevMonth"
tools:text="JANUARY 2019" />
<ImageButton
android:id="@id/nextMonth"
style="@style/CalendarRightArrowButton"
android:contentDescription="@string/next_month" />
</RelativeLayout>
styles.xml 的相关部分:
<style name="CalendarArrowButtons">
<item name="android:layout_width">44dp</item>
<item name="android:layout_height">44dp</item>
</style>
<style name="CalendarLeftArrowButton" parent="CalendarArrowButtons">
<item name="android:layout_alignParentLeft">true</item>
<item name="android:background">@drawable/left_arrow_vector</item>
</style>
<style name="CalendarRightArrowButton" parent="CalendarArrowButtons">
<item name="android:layout_alignParentRight">true</item>
<item name="android:background">@drawable/right_arrow_vector</item>
</style>
当我删除包含并直接使用其中一个时,它会在各个版本上正确显示(好吧,正常布局在 19 上崩溃,但这是意料之中的)。
【问题讨论】:
标签: android android-layout android-api-levels