jjzjj

android - PercentRelativeLayout 在 v26 中被弃用

coder 2023-12-19 原文

我想显示一个包含 6 个 View 的布局,我按宽度和高度百分比进行设置。 但是 PercentageLayout 在 Android v26 中被弃用了 PercentageLayout 的替代方案是什么。 Android 文档说使用 ConstraintLayoutapp:layout_constraintGuide_percent。 ( https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html ) 但是对于 PercentageLayout 我们可以使用

 app:layout_heightPercent="33.4%"
    app:layout_widthPercent="45%"

最佳答案

ConstraintLayout 有两种实现布局的方法。

#1 准则

考虑到您需要布置六个 View ,您需要添加两个垂直指南,其百分比位置为父级宽度的 33%66%。并且您还需要添加两个水平指南,其百分比位置为屏幕高度的 45%90%。然后将您的六个 View 限制在这些准则中,如屏幕截图所示:

这里是布局构建指南的 XML 源代码:

<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">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_1"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_1"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_purple"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_2"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/horizontal_guideline_1" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_2"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintTop_toTopOf="@+id/horizontal_guideline_1" />

    <View
        android:id="@+id/view6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintTop_toTopOf="@+id/horizontal_guideline_1" />

    <android.support.constraint.Guideline
        android:id="@+id/vertical_guideline_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.33" />

    <android.support.constraint.Guideline
        android:id="@+id/vertical_guideline_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.66" />

    <android.support.constraint.Guideline
        android:id="@+id/horizontal_guideline_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <android.support.constraint.Guideline
        android:id="@+id/horizontal_guideline_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9" />

</android.support.constraint.ConstraintLayout>

#2 有重量的链

Chains 是另一种使用百分比值调整 View 大小的强大方法。您可以像在 LinearLayout 中使用权重一样在 ConstraintLayout 链中使用权重值。

下面的 XML 示例展示了如何使用带权重的链:

<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">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintVertical_weight="45"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view2"
        app:layout_constraintBottom_toTopOf="@+id/view4" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view1"
        app:layout_constraintStart_toEndOf="@+id/view1"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintBottom_toBottomOf="@+id/view1" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_purple"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view2"
        app:layout_constraintStart_toEndOf="@+id/view2"
        app:layout_constraintBottom_toBottomOf="@+id/view2"
        app:layout_constraintEnd_toEndOf="parent" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintVertical_weight="45"
        app:layout_constraintTop_toBottomOf="@+id/view1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view5"
        app:layout_constraintBottom_toTopOf="@+id/space" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view4"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintEnd_toStartOf="@+id/view6"
        app:layout_constraintBottom_toBottomOf="@+id/view4" />

    <View
        android:id="@+id/view6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view5"
        app:layout_constraintStart_toEndOf="@+id/view5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/view5" />

    <Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="10"
        app:layout_constraintTop_toBottomOf="@+id/view4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

上面显示的两个例子都会导致这个结果:

阅读更多关于:

关于android - PercentRelativeLayout 在 v26 中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47869182/

有关android - PercentRelativeLayout 在 v26 中被弃用的更多相关文章

  1. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby - 有没有办法让 2.4.0 中的 Ruby 弃用警告静音? - 2

    从Ruby2.4.0开始,对于使用某些已弃用的功能,会出现弃用警告。例如,Bignum、Fixnum、TRUE和FALSE都会触发弃用警告。当我修复我的代码时,有相当多的代码我希望它保持沉默,尤其是在Rails中。我该怎么做? 最佳答案 moduleKerneldefsuppress_warningsoriginal_verbosity=$VERBOSE$VERBOSE=nilresult=yield$VERBOSE=original_verbosityreturnresultendend>>X=:foo=>:foo>>X=:bar

  4. ruby-on-rails - Ruby on Rails 教程 - 5.26 - Sublime Text "Unable to Save"新文件 "spec/support/utilities.rb" - 2

    我正在使用SublimeText2,同时遵循MichaelHartl的RubyonRails教程。可以在http://ruby.railstutorial.org/book/ruby-on-rails-tutorial找到我所指的教程的具体部分。(ctrl+F“list5.26”)。我能够创建规范/支持文件。但是,在尝试创建spec/support/utilities.rb文件时,我收到消息“无法保存~/rails_projects/sample_app/spec/support/utilities.rb”。有人知道为什么会这样吗?SublimeText论坛上有人似乎遇到了完全相同的问

  5. 【历史上的今天】4 月 26 日:验证码的发明者诞生;切尔诺贝利病毒爆发;诺基亚收购 Withings - 2

    整理|王启隆透过「历史上的今天」,从过去看未来,从现在亦可以改变未来。今天是2023年4月26日,在2017年的今天,中国首艘国产001A型航空母舰在大连完成了下水,从开工到下水,历时3年多时间。回首过去,眺望未来,在科技历史上的每个4月26日里,还发生过哪些影响深远的关键事件呢?1938年4月26日:编程校验领域图灵奖得主ManuelBlum出生曼纽尔·布卢姆(ManuelBlum)出生于1938年4月26日,他是委内瑞拉的计算机科学家、卡内基梅隆大学的教授,因对计算复杂度理论做出的贡献,以及在密码学和编程校验上的应用而获1995年图灵奖。布卢姆出生于委内瑞拉的一个犹太家庭,他曾在麻省理工学

  6. ruby-on-rails - 带有 redirect_to 的 Flash 通知在 rails 中被破坏 - 2

    我已更新到Rails2.3.10、Rack1.2.1,现在我的所有即时消息都没有显示。我发现在重定向期间,通知是这样传递的redirect_to(@user,:notice=>"Sorrytherewasanerror")在我看来闪存哈希是空的!map:ActionController::Flash::FlashHash{}但是您可以在Controller中看到该消息。是什么原因?session{:home_zip=>"94108",:session_id=>"xxx",:flash=>{:notice=>"Sorrytherewasanerror"},:user_credential

  7. ruby-on-rails - 检查模型是否在 before_save 事件中被修改或创建 - 2

    我想检查是否正在Rails的before_save回调中创建模型。我还想检查它是否已被修改(更新时)。谢谢 最佳答案 您可以使用new_record?看看你是否有一个全新的对象和changed?查看是否有任何变化:before_save:pancakesdefpancakesifnew_record?#Notinthedatabaseyet.elsifchanged?#Alreadyexistsbutithasunsavedchanges.endend 关于ruby-on-rails-检

  8. ruby-on-rails - 从 gem 生成的静音弃用警告 - 2

    我正在使用unscoped_associations我的Rails5.0.0.1应用程序中的gem。我收到这个弃用警告:DEPRECATIONWARNING:alias_method_chainisdeprecated.Please,useModule#prependinstead.Frommodule,youcanaccesstheoriginalmethodusingsuper.(calledfromat/home/rhl/myapp/config/application.rb:8)DEPRECATIONWARNING:alias_method_chainisdeprecated.

  9. ruby-on-rails - Rails group_by 是否已弃用? - 2

    我有一个要分组的数组,“group_by”函数似乎适合我的情况。http://apidock.com/rails/Enumerable/group_by我在Rails3.2.13中使用它。grouped_array=my_array.group_by(&:my_function)#Assumerun'my_function'haveresult1onelement1,element3andresult2onelement2,element4,then:#grouped_array={#result1=>[element1,element3],#result2=>[element2,el

  10. ruby-on-rails - 验证事件连接!在 Rails 4 中已弃用,我们应该如何处理该功能? - 2

    我一直在关注这篇文章以与工头一起设置puma:https://www.digitalocean.com/community/articles/how-to-set-up-zero-downtime-rails-deploys-using-puma-and-foremanpuma脚本在连接后告诉verify_active_connections!但它在rails4中不可用。注释掉方法调用将使脚本运行但我不确定这是否会泄漏资源.关于这个问题,我能看到的唯一文档是:https://github.com/socialcast/resque-ensure-connected/issues/3但是

随机推荐