android – 约束布局,百分比无法按预期工作

android – 约束布局,百分比无法按预期工作,第1张

概述我会创建一个宽度为70%的视图,并使用约束布局对齐其父级的右侧,如下所示 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 我会创建一个宽度为70%的视图,并使用约束布局对齐其父级的右侧,如下所示

<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="wrap_content"    >    <androID.support.constraint.GuIDeline        androID:ID="@+ID/guIDeline"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:orIEntation="horizontal"        app:layout_constraintGuIDe_percent="0.3"/>    <TextVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="40dp"        androID:text="Hello text"        app:layout_constraintleft_toRightOf="@+ID/guIDeline"        app:layout_constraintRight_toRightOf="parent"/></androID.support.constraint.ConstraintLayout>

TextVIEw始终占用完整的父宽度.知道我做错了什么吗?

解决方法 两个小但重要的变化:

> TextVIEw宽度应为0dp,即匹配约束而不匹配父级
>指导方向应垂直而非水平

这是代码:

<?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">    <androID.support.constraint.GuIDeline        androID:ID="@+ID/guIDeline"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:orIEntation="vertical"        app:layout_constraintGuIDe_percent="0.3" />    <TextVIEw        androID:layout_wIDth="0dp"        androID:layout_height="wrap_content"        androID:text="Hello text"        app:layout_constraintleft_toRightOf="@ID/guIDeline"        app:layout_constraintRight_toRightOf="parent" /></androID.support.constraint.ConstraintLayout>

输出:

另请注意,我将ConstraintLayout高度更改为match_parent,以便在输出中显示指南.您可以将其更改回wrap_content.

总结

以上是内存溢出为你收集整理的android – 约束布局,百分比无法按预期工作全部内容,希望文章能够帮你解决android – 约束布局,百分比无法按预期工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1122581.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存