gpt4 book ai didi

android - GridView水平间距错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:13 24 4
gpt4 key购买 nike

我在我的 XML 布局文件中放置了一个 GridView。

它在 API 17-21 上以我想要的英语 (LTR) 显示但是,当我将语言转换为阿拉伯语 (RTL) 时,API 17-19 上的 horizo​​ntalSpacing 会被忽略,但在 API 21 上它会按我的需要显示。

相同的行为发生在模拟器和真实的 Android 设备上。

XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/services_home_main_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/services_home_contents"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/services_home_bottom_layout">

<GridView
android:id="@+id/services_home_services_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:layout_margin="4dp"
android:gravity="center"
android:choiceMode="singleChoice" />

</LinearLayout>

<LinearLayout
android:id="@id/services_home_bottom_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />

</RelativeLayout>

项目的 XML 代码:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/services_drawer_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="4dp"
android:background="@color/Gray_CC"
android:drawableTop="@drawable/tall4"
android:text="@string/app_name" />

这是我的阿拉伯语 (RTL) UI 屏幕截图:

注意第一个屏幕截图中的水平间距

enter image description here

最佳答案

试试这个,我不知道为什么但对我有用

if(activity.getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
gridView.setHorizontalSpacing((int) -padding);
else
gridView.setHorizontalSpacing((int) padding);

关于android - GridView水平间距错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27719638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com