在 android 中创建响应式视图
Make responsive views in android
我在 android 响应式设计方面遇到了很多麻烦。在网页设计中,我们可以使用百分比作为视图大小,但在 android 中我们没有这样的东西。
无论我使用 ImageButtons 或普通按钮还是其他任何东西,当我更改 "device for preview" 时,我的孔设计都会很乱,因为宽度和高度不会动态变化,例如,我让 Pixel 设备上的一切看起来都很好,但是当我改变我的预览到 Pixel 2 XL 每样东西都是那么小。我打赌你不想知道当我把预览改成 wear 时会发生什么 :D
我应该提一下,我知道我不能使用硬编码值,而且我并没有经常这样做。
我使用 Adobe XD 设计了我的按钮,使用该工具我可以导出 PNG 类型和六种不同尺寸(ldpi、mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi)的按钮。但即使是我的 ImageButtons 也没有动态改变它们的大小。
我试图找到解决方案,我确信一定有解决方案,但也许是因为我的英语不好,当我搜索 android 响应式设计或类似的东西时,我得到了一些不相关的结果。
非常感谢您的帮助
下面是一些示例代码,ImageButtons 的大小不会动态变化,因此在像 Pixel 这样的小型设备中,我具有滚动功能,但在像 Pixel 3 XL 这样的大型设备中,按钮非常小,因此无需滚动。
<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/button" android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" android:scrollbars="none"
>
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent">
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/all_foods_btn" android:background="@android:color/transparent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/main_activity_all_foods_btn_dsc"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/profile_btn_main" android:background="@android:color/transparent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/main_activity_profile_btn_dsc"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/classic_decider_btn" android:background="@android:color/transparent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/main_activity_classic_decider_btn_dsc"/>
</LinearLayout>
</HorizontalScrollView>
尝试将 android:layout_height="0dp"
更改为 HorizontalScrollView
。这将使它尊重您设置的约束(底部到父级和顶部到 button
)
try this library:此尺寸单位根据 sp 尺寸单位(对于文本)随屏幕尺寸缩放。它可以帮助 Android 开发人员支持多屏幕。
我在 android 响应式设计方面遇到了很多麻烦。在网页设计中,我们可以使用百分比作为视图大小,但在 android 中我们没有这样的东西。 无论我使用 ImageButtons 或普通按钮还是其他任何东西,当我更改 "device for preview" 时,我的孔设计都会很乱,因为宽度和高度不会动态变化,例如,我让 Pixel 设备上的一切看起来都很好,但是当我改变我的预览到 Pixel 2 XL 每样东西都是那么小。我打赌你不想知道当我把预览改成 wear 时会发生什么 :D
我应该提一下,我知道我不能使用硬编码值,而且我并没有经常这样做。 我使用 Adobe XD 设计了我的按钮,使用该工具我可以导出 PNG 类型和六种不同尺寸(ldpi、mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi)的按钮。但即使是我的 ImageButtons 也没有动态改变它们的大小。 我试图找到解决方案,我确信一定有解决方案,但也许是因为我的英语不好,当我搜索 android 响应式设计或类似的东西时,我得到了一些不相关的结果。
非常感谢您的帮助
下面是一些示例代码,ImageButtons 的大小不会动态变化,因此在像 Pixel 这样的小型设备中,我具有滚动功能,但在像 Pixel 3 XL 这样的大型设备中,按钮非常小,因此无需滚动。
<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/button" android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" android:scrollbars="none"
>
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent">
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/all_foods_btn" android:background="@android:color/transparent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/main_activity_all_foods_btn_dsc"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/profile_btn_main" android:background="@android:color/transparent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/main_activity_profile_btn_dsc"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/classic_decider_btn" android:background="@android:color/transparent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/main_activity_classic_decider_btn_dsc"/>
</LinearLayout>
</HorizontalScrollView>
尝试将 android:layout_height="0dp"
更改为 HorizontalScrollView
。这将使它尊重您设置的约束(底部到父级和顶部到 button
)
try this library:此尺寸单位根据 sp 尺寸单位(对于文本)随屏幕尺寸缩放。它可以帮助 Android 开发人员支持多屏幕。