Android 如果父项超过650dp,则让Linearlayout width为650dp,否则匹配父项

Android 如果父项超过650dp,则让Linearlayout width为650dp,否则匹配父项,android,android-layout,Android,Android Layout,我正在FrameLayout内创建LinearLayout,LinearLayout需要在FrameLayout内居中,但LinearLayout的宽度需要更改如下: 如果父FrameLayout超过650dp,则将LinearLayout width设置为650dp,这会在线性布局的左右两侧留下一些边距 如果父帧布局低于650dp,则将LinerYout宽度设置为与父帧匹配 父框架布局宽度将随着方向的改变而改变,我尝试使用OnLayoutChangeListener、OnGlobalYoutL

我正在FrameLayout内创建LinearLayout,LinearLayout需要在FrameLayout内居中,但LinearLayout的宽度需要更改如下: 如果父FrameLayout超过650dp,则将LinearLayout width设置为650dp,这会在线性布局的左右两侧留下一些边距

如果父帧布局低于650dp,则将LinerYout宽度设置为与父帧匹配

父框架布局宽度将随着方向的改变而改变,我尝试使用OnLayoutChangeListener、OnGlobalYoutListener、OnAttachStateChangeListener在屏幕旋转后检测宽度,但它们都不起作用

通过尝试查找父视图的宽度,我是否在正确的轨道上

或者,是否可以将父视图用作约束视图,并在可能的情况下使线性布局在内部,并在宽度不足时扩展到父视图


(注意,我不能在不同的layout res文件夹(如sw600或sw600 land)下使用不同的值,因为parentview不一定占据整个屏幕,它可能只是屏幕上两列中的一列)处理此问题的适当方法是在
布局栏
下创建另一个布局文件,并使用650dp宽度。关于支持不同屏幕的更多说明。

对于任何感兴趣的人,我根据Eugen的评论通过一个约束列表解决了这个问题。这将确保LinearLayout的宽度与较小屏幕上的父屏幕匹配(

当父级宽度=150dp时的结果


问题在于,我不能假设屏幕宽度等于父视图宽度,例如,父框架布局可能位于tablet横向视图的两列之一。在这种情况下,即使屏幕宽度为sw720,我可能仍需要使其与父视图匹配。不过,您可以应用我在宽度限定符中提到的相同逻辑。这意味着您将d创建一个新的
styles.xml
,并给它一个650dp的宽度限定符,在那里你可以定义线性布局的宽度。你应该清楚地看到如何在上面的同一链接中为不同的宽度创建不同的样式。如果我在sw720中定义一个650的样式,并且在720宽度的屏幕上有一个两列视图,那么该列可能只是宽度为300dp,在这种情况下,设置650将使视图比
ConstraintLayout
(非
CoordinatorLayout
)的屏幕子级更宽,可以指定
app:layout\u constraintWidth\u max
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">

  <LinearLayout
      android:id="@+id/row_content"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintWidth_max="650dp"/>
</android.support.constraint.ConstraintLayout>
<LinearLayout
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:background="#f00"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:maxWidth="200dp"
        android:minWidth="200dp"
        android:background="#00f"
        >
    </LinearLayout>
</LinearLayout>