android - 约束流小部件忽略最大宽度属性
问题描述
我有多个布局,我想根据屏幕大小在不同的位置显示。如果屏幕足够宽(平板电脑),我希望布局水平显示然后换行到下一行。如果屏幕很窄(电话),我希望布局垂直堆叠。
当我提供固定的 layout_width 时,约束流小部件可以正常工作。
我想将子容器的布局更改为动态的……达到极限。将容器的内容散开仅在一定程度上有用。基本上,当在手机上显示时,我希望容器的大小适应屏幕大小。在平板电脑或大型手机/肖像上显示时,请限制宽度,以免占用整个屏幕。
为此,我设置了 android:layout_width="match_parent" 和 app:layout_constraintWidth_max="350dp"。该行为在其他布局容器中正常工作,但不适用于 Flow 小部件的约束。Flow 小部件忽略最大宽度属性并依赖于 layout_width。第二个布局容器被推离屏幕右侧。当鼠标悬停在第二个布局上时,可以在 Android Studio 中看到第二个布局的轮廓。
如何实现动态布局大小和布局的目标?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:maxWidth="350dp"
android:minHeight="490dp"
app:layout_constraintWidth_max="350dp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/layoutTwo"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_purple"
android:minHeight="490dp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/layoutThree"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:minHeight="490dp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/layoutFour"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:minHeight="490dp" />
<androidx.constraintlayout.helper.widget.Flow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:constraint_referenced_ids="layoutOne,layoutTwo,layoutThree,layoutFour"
app:flow_horizontalStyle="packed"
app:flow_verticalAlign="top"
app:flow_wrapMode="chain"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
解决方案
这就是我解决它的方法。在 onCreate() 中,读取最初在 XML 中设置的宽度值。如果布局宽度超过屏幕宽度,则布局宽度设置为屏幕宽度。
我尝试以编程方式设置 matchConstraintMaxWidth,但它不起作用。布局不流动/包装。
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)myConstraintLayout.getLayoutParams();
if(displayMetrics.widthPixels < (int) layoutParams.width )
layoutParams.width = displayMetrics.widthPixels;
myConstraintLayout.setLayoutParams(layoutParams);
推荐阅读
- google-apps-script - 无法使用 Apps 脚本从团队云端硬盘中删除文件
- javascript - Setting cookie in iframe when using safari
- python - Fastest way to draw pixels to screen in python
- xslt - 使用 XSLT 移动和重命名 KML 节点
- python - Python绘制条形图求和一列,按两个不同的列分组
- physics - 如何解决 proc 0 上的错误:无法打开文件 tersoff.data (../read_data.cpp:1938)?
- apache-kafka - Kafka Connect - 无法刷新,等待生产者刷新未完成的消息时超时
- python - 从我的服务器下载文件以将服务器跳转到我的笔记本电脑
- reactjs - Microsoft Edge isPersonal SavePersonalAndPaymentData 刹车 React 应用
- c# - 从 HttpRequestMessage 获取 Route 参数