我是ConstraintLayout的新手,我正在尝试使用ConstraintLayout复制GridLayout提供的相同网格行为.
具体来说,我想设计一个两列网格.第一列宽度应尽可能窄,而第二列应占用所有剩余的水平空间.当然,第二列应该位于第一列的右侧,或者更确切地说,位于第一列的最宽视图.
我不知道如何使用ConstraintLayout复制最后一个要求.我不想在两列之间使用网格线,因为第一列不应该具有固定的宽度和百分比宽度,而是应该与其最宽的视图一样宽.
在https://gist.github.com/venator85/499dd82f47b3efbbed7a1e9e1ca1412d我准备了一个布局示例和相应的预览,显示了一个实现我想要的GridLayout.在该布局中的前两个ConstraintLayout尝试显示C1和D1与B1对齐,C2和D2与B2对齐.当B2比A2窄时,A1和C1将重叠.
有帮助吗?
谢谢
android android-layout android-gridlayout android-constraintlayout
我遇到Android Studio问题.我最近升级了我的SDK Manager以包含;
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
Run Code Online (Sandbox Code Playgroud)
classpath是
classpath 'com.android.tools.build:gradle:2.2.3'
Run Code Online (Sandbox Code Playgroud)
项目建设很好.Intellisense和文档工作正常.但是,当我尝试在手机上运行(调试)应用程序时,我收到此错误;
Caused by: android.view.InflateException: Binary XML file line #2:
Binary XML file line #2: Error inflating class ConstraintLayout
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:443)
at android.app.Activity.setContentView(Activity.java:2172)
Run Code Online (Sandbox Code Playgroud)
我已经同步了.我重新启动了Android Studio.我已经清理并重建了这个项目.
有想法该怎么解决这个吗?谢谢!
-edit- XML看起来像这样;
<ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/PLAY_PARENT"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/greenfelt2"
android:padding="0dp">
Run Code Online (Sandbox Code Playgroud) TD; DR
视图宽度必须恰好是屏幕的一半,并且居中.使用ConstraintLayout.
请注意,视图没有任何内部宽度.
<View android:background="#ff0000" ... />
Run Code Online (Sandbox Code Playgroud)
原始问题
我想实现一个布局,其中视图大小是屏幕大小的一半,并且水平居中.
这样的事情:| --view-- |
我找不到任何使用ConstraintLayout的方法.我找到的最好的是分别使用app:layout_constraintHorizontal_weight="1"位于完整左侧和右侧的2个假视图,以及app:layout_constraintHorizontal_weight="1.5"我的视图.
有更好的方法吗?
我在约束布局中有3个指南.一个是16dp左边缘.第二是16dp右边缘.但我想要另一个指南,它将在中心.假设如果我在Android Stdio XML设计面板的nexus 5中创建此指南中心,那么在nexus S中它不会显示在中心.怎么解决这个?
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.blabla.ScrollingActivity">
<android.support.constraint.Guideline
android:id="@+id/guideline_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="16dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="16dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="180dp"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud) 如果我只有一个带有几个项目的 LinearLayout,那么用 ConstraintLayout 替换它会有什么效果
<LinearLayout>
<TextView>
<TextView>
<TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
对比
<ConstraintLayout>
<TextView>
<TextView>
<TextView>
</ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
它们的效率有什么不同吗?
在ConstraintLayout我们有新类型的参数creator:layout_constraintTop_creator或layout_constraintRight_creator
如何使用它?
我使用了constraintLayout和layout_constraintDimensionRatio ="1:1"(宽度为warp_content,高度为0dp(match_constraint))
因此,我预计宽度和高度为1:1但不起作用.
怎么了??
我附上了代码和截图.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center"
android:text="Hello World!11"
app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我引用Android开发者网站关于Constraintlayout. https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints
"比率::您还可以将窗口小部件的一个维度定义为另一个维度的比率.为此,您需要将至少一个约束维度设置为0dp(即MATCH_CONSTRAINT),并设置属性layout_constraintDimentionRatio到给定的比率.例如:
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
Run Code Online (Sandbox Code Playgroud)
将按钮的高度设置为与其宽度相同."
- >但我没有工作.
我有一个ConstraintLayout内部NestedScrollView.在ConstraintLayout包含了一堆的意见,但最后View能有一个动态的高度,填补了底部空间,当且仅当有什么,但它也需要一个最小高度,如果没有足够的空间.
为了论证,这是一个例子.
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_min="1500dp"
android:background="@color/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我已将该ConstraintLayout版本放入,但它不起作用.显然,这些值非常大,但这只是用于测试.
如果我不设置fillViewport="true"上NestedScrollView则ConstraintLayout具有0高度时,我设置的fillViewport,在ConstraintLayout不滚动,但只是充满屏幕.
如何设置View以使其扩展到ConstraintLayout应该与Viewport一样大的底部,但如果我的View不是minHeight那么我们允许滚动?
我正在使用1.0.2该ConstraintLayout库的版本.
我期望看到的是一直到父母的底部,但如果那个大小小于1500dp那么视图滚动.
我已经输入1500dp android:layout_height="1500dp",视图会相应滚动.
更新1
似乎是我把布局放在一个FragmentViewPager.该app:layout_constraintHeight_min属性不受尊重,只与视口的高度匹配.
我也尝试NestedScrollView从片段中取出并将其ViewPager放入其中但是再次无效.
我正在尝试使用ConstraintLayout创建布局组合.为了简化我的案例,我的布局应该有三个部分:
我正在努力的部分是为第一个布局设置最大高度(红色).好像ConstraintLayout忽略了我的"最大高度语句":
app:layout_constraintHeight_max="300dp"
Run Code Online (Sandbox Code Playgroud)
这是我当前的结果(红色部分忽略了高度限制..):
这是完整的XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.mixtiles.android.reviewOrder.ReviewOrderActivity"
tools:layout_editor_absoluteY="25dp">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/review_order_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/red"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/red"
app:layout_constraintBottom_toTopOf="@+id/green"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
app:layout_constraintVertical_chainStyle="spread_inside">
</FrameLayout>
<FrameLayout
android:id="@+id/green"
android:layout_width="0dp"
android:layout_height="150dp"
android:background="@color/greenish"
app:layout_constraintBottom_toTopOf="@+id/pink"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/red">
</FrameLayout>
<FrameLayout
android:id="@+id/pink"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/pink"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/green">
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
我正在努力实现这个结果:
我现在所拥有的是ConstraintLayout包括三个Views:TextView,ImageView,ImageView
设计要求:
TextView;TextView 固定大小为80dpImageViews应根据文本位置水平改变位置TextView.请参见案例#1 /#2的图像以供参考.是否可以用XML实现第4项?那么以编程方式呢?
[附加信息]
自动调整大小无法正常工作wrap_content,请参阅自动调整文档:
注意:如果在XML文件中设置自动调整大小,则不建议对TextView
layout_width或layout_height属性使用值"wrap_content" .它可能会产生意外的结果.
android textview autoresize imageview android-constraintlayout