java - layout_constraintGuide_percent 不起作用 - android
问题描述
我正在使用ConstraintLayout
. 我想将页面分成两种颜色,并且我想在整个屏幕上拥有一个 70% 高度的布局。
这是我的代码:
<?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="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical" >
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:cardBackgroundColor="@color/wikitude_primary">
</android.support.v7.widget.CardView>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#fff"></LinearLayout>
</LinearLayout>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/guideline"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8"/>
</android.support.constraint.ConstraintLayout>
但app:layout_constraintGuide_percent
不起作用。
我想做出这样的看法:
解决方案
您的布局几乎没有问题。最值得注意的是,您没有将约束设置为准则。尝试在 Android Studio 设计器中生成此图像的以下布局。我添加了文本视图来标识这些部分,但其他情况下不需要它们。
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:weightSum="2"
app:layout_constraintBottom_toTopOf="@id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:cardBackgroundColor="@color/wikitude_primary">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CardView"
android:textSize="24sp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom LinearLayout"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
</android.support.constraint.ConstraintLayout>
推荐阅读
- c++ - 为什么从 QFileDialog::accept() 打开的 QMessageBox 在 macOS 上没有保持在顶部?
- go - 如何读取 Volumesnapshot 对象(或任何 K8s 对象)
- c# - 无法在两端使用相同的类在 C# 中反序列化 JSON
- javascript - 迭代变量 (JavaScript)
- python - 线程异步函数python
- java - 从字符串中找到第一个元音并将其添加到 var
- colors - 在很棒的 wm 容器小部件上切换两种颜色
- r - 如何停止粘贴功能在闪亮的输出中重复?
- javascript - Vue 只允许数组中的一个元素为真
- python - Matplotlib 没有出现在 ubuntu windows 子系统上