随着Android Studio 2.2的预览1,谷歌在其支持库中发布了一个新的布局:ConstraintLayout。使用ConstraintLayout,在Android Studio中更容易使用设计工具,但我没有找到一种方法来使用相对大小(百分比或“权重”,如线性布局)。是否有一种基于百分比定义约束的方法?例如,让一个视图占屏幕的40%,在视图之间创建20%的空白,将一个视图的宽度设置为另一个视图宽度的50% ?
当前回答
我知道这不是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_constraintHeight_percent和app:layout_constraintWidth_percent来改变高度和宽度百分比。
<?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="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FF00FF"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent=".6"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".4"></LinearLayout>
</android.support.constraint.ConstraintLayout>
Gradle:
dependencies {
...
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
约束布局1.0使一个视图占据屏幕的百分比需要制定两个指南。在约束布局1.1中,通过允许您轻松地将任何视图限制为一个百分比的宽度或高度,它变得更加简单。
这不是很棒吗?所有视图都支持layout_constraintWidth_percent和layout_constraintHeight_percent属性。这将导致约束固定在可用空间的一个百分比上。因此,让按钮或TextView扩展以填充屏幕的百分比可以用几行XML完成。
例如,如果你想设置按钮的宽度为屏幕的70%,你可以这样做:
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_constraintWidth_percent="0.7" />
请注意,你将不得不把尺寸应该使用百分比到0dp,因为我们已经指定android:layout_width到0dp上面。
同样,如果你想设置按钮的高度为屏幕的20%,你可以这样做:
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_constraintHeight_percent="0.2" />
看!我们已经指定android:layout_height为0dp这次,因为我们想按钮使用高度百分比。
简单地,在指导标签中替换
app:layout_constraintGuide_begin="291dp"
with
app:layout_constraintGuide_percent="0.7"
其中0.7表示70%。
另外,如果您现在尝试拖动指导方针,拖动的值现在将以%age显示。
目前有几种方法可以做到这一点。
一种是创建导线(右键单击设计区域,然后单击添加垂直/水平导线)。然后,您可以单击指导方针的“标题”来更改定位为基于百分比。最后,您可以将视图约束为指导方针。
另一种方法是使用偏差(百分比)定位视图,然后将其他视图锚定在该视图上。
也就是说,我们一直在考虑如何提供基于百分比的维度。我不能做任何保证,但这是我们想补充的。
使用指南,您可以将定位更改为基于百分比
<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"
推荐文章
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?