gpt4 book ai didi

Nougat 7.0 API 24 和 7.1 API 25 中的 Android 布局间距发生了变化

转载 作者:行者123 更新时间:2023-11-29 02:41:06 25 4
gpt4 key购买 nike

我有一个 fragment ,它在顶部包含另一个 fragment ,然后是一个有两列的水平 LinearLayout,总 weightSum=3。左列是一个垂直的 RelativeLayout(权重为 1),带有一个 TextView 和一个 ListView。右边是一个垂直的 LinearLayout(权重为 2),带有一个 TextView 和一个 GridView 以及一个 Button。牛轧糖的间距发生了巨大变化。在 Studio 模拟器和我自己的 Samsung S7 Edge 中。此外,模拟器报告不同的屏幕布局。对于 Nexus 6P (1440 x 2560),API 23 报告大,而 API 25 报告正常。我的三星 s7 Edge(以及 1440 x 2560)也报告正常并且正在运行 7.0 (API 24)。

在 API 24 及更高版本中,正确的布局被紧紧地挤压到右边缘,就好像权重被忽略了一样。

API 23 工作正常。我使用了相同的模拟器类型,6P 和不同的 API。

这是 API 23 的样子: enter image description here

这是 API 25 的样子:

enter image description here

这是我的 fragment :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_course"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hubbardsoftware.racetac.CourseActivityFragment"
tools:showIn="@layout/activity_course">

<fragment
android:name="com.hubbardsoftware.racetac.TimerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_timer" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">

<RelativeLayout
android:id="@+id/course_part"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/course_course_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/title_course"
android:gravity="center"
android:textSize="@dimen/text_course_title"
android:textColor="#ff000000"
android:textStyle="bold" />

<ListView
android:id="@+id/listview_course"
android:layout_below="@id/course_course_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</RelativeLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_toEndOf="@id/course_part"
android:layout_toRightOf="@id/course_part"
android:layout_gravity="right"
android:orientation="vertical">

<TextView
android:id="@+id/course_marks_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="@string/title_marks"
android:textSize="@dimen/text_course_title"
android:textColor="#ff000000"
android:textStyle="bold" />

<GridView
android:id="@+id/gridview_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />

<Button
android:id="@+id/button_clear_course"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_rounded"
android:backgroundTint="@color/clearCourseButton"
android:text="@string/clear_all"
android:textSize="@dimen/text_course_clear"
android:textStyle="bold" />

<TextView
android:id="@+id/course_length"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Length"
android:textSize="@dimen/text_course_length"
android:textColor="#ff000000"
android:textStyle="bold" />

</LinearLayout>

</LinearLayout>

关于为什么会发生变化有什么想法吗?对于这种尺寸的设备,这似乎与 API 24 和 25 中的以下情况有关:

((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)

而它等于 API 23 中的 Configuration.SCREENLAYOUT_SIZE_LARGE。

最佳答案

这个 Java 代码解决方法解决了这个问题,但如果有人知道的话,我仍然想要一个合适的 XML 解决方案。我保留了上面的 XML,RelativeLayout 的宽度为 200dp。然后我在 fragment onCreateView 方法中执行了以下操作:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
RelativeLayout coursePart = (RelativeLayout) rootView.findViewById(R.id.course_part);
ViewGroup.LayoutParams params = coursePart.getLayoutParams();
params.width = 0;
}

新旧 API 使用此代码显示相同。

关于Nougat 7.0 API 24 和 7.1 API 25 中的 Android 布局间距发生了变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055564/

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