随着Android Studio 2.2的预览1,谷歌在其支持库中发布了一个新的布局:ConstraintLayout。使用ConstraintLayout,在Android Studio中更容易使用设计工具,但我没有找到一种方法来使用相对大小(百分比或“权重”,如线性布局)。是否有一种基于百分比定义约束的方法?例如,让一个视图占屏幕的40%,在视图之间创建20%的空白,将一个视图的宽度设置为另一个视图宽度的50% ?
当前回答
目前有几种方法可以做到这一点。
一种是创建导线(右键单击设计区域,然后单击添加垂直/水平导线)。然后,您可以单击指导方针的“标题”来更改定位为基于百分比。最后,您可以将视图约束为指导方针。
另一种方法是使用偏差(百分比)定位视图,然后将其他视图锚定在该视图上。
也就是说,我们一直在考虑如何提供基于百分比的维度。我不能做任何保证,但这是我们想补充的。
其他回答
使用指南,您可以将定位更改为基于百分比
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
你也可以用这种方式
android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.4"
目前有几种方法可以做到这一点。
一种是创建导线(右键单击设计区域,然后单击添加垂直/水平导线)。然后,您可以单击指导方针的“标题”来更改定位为基于百分比。最后,您可以将视图约束为指导方针。
另一种方法是使用偏差(百分比)定位视图,然后将其他视图锚定在该视图上。
也就是说,我们一直在考虑如何提供基于百分比的维度。我不能做任何保证,但这是我们想补充的。
简单地,在指导标签中替换
app:layout_constraintGuide_begin="291dp"
with
app:layout_constraintGuide_percent="0.7"
其中0.7表示70%。
另外,如果您现在尝试拖动指导方针,拖动的值现在将以%age显示。
我知道这不是OP最初要求的,但在这种情况下,当我有类似的问题时,这帮助了我很多。添加这为人们希望改变布局窗口大小(我经常使用),通过代码。将此添加到您的onCreate活动的问题。(改为80%)
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width * 0.8), (int)(height * 0.8));
你可以使用app:layout_constraintVertical_weight,它与linearlayout中的layout_weight相同
<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">
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button5"
app:layout_constraintVertical_weight="1"/>
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintLeft_toRightOf="@+id/button4"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_weight="1"/>
</android.support.constraint.ConstraintLayout>
注意:app:layout_constraintVertical_weight(app:layout_constraintHorizontal_weight)将与android:layout_width="0dp" (android:layout_height="0dp"
推荐文章
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态