gpt4 book ai didi

android-constraintlayout - 如何在指南中将 View 居中?

转载 作者:行者123 更新时间:2023-12-04 05:36:22 24 4
gpt4 key购买 nike

假设我有一个水平 GuideLine在父级的 30% 和一个 View (假设一个按钮)处,如何使该 View 以指南为中心?

像这样:

enter image description here

更新:

我在这里使用的 View 具有基于比率的高度,并且答案的解决方案不起作用。

这是当前的布局:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imgLogo"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/guideline_30"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="@+id/guidelineV_75"
app:layout_constraintStart_toEndOf="@+id/guidelineV_25"
app:layout_constraintTop_toTopOf="@+id/guideline_30"
app:srcCompat="@drawable/ic_launcher_background" />

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

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

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

</android.support.constraint.ConstraintLayout>

最佳答案

Using fix width or height



需要使用相同的 Guideline对于顶部和底部约束,如:
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="@id/guideline_30"

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

<ImageView
android:id="@+id/imgLogo"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="@id/guideline_30"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/splash_logo" />

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

</android.support.constraint.ConstraintLayout>

输出:

enter image description here

Using width and height based on ratio


  • 当您根据比率使用宽度和高度时,它不会居中。
  • 但是为了得到你想要的结果,你也可以设置 Guideline = 0.15 (0.3/2) 并修改 constraintTopconstraintBotttom

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

    <ImageView
    android:id="@+id/imgLogo"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H,1:1"
    app:layout_constraintEnd_toStartOf="@+id/guidelineV_75"
    app:layout_constraintStart_toEndOf="@+id/guidelineV_25"
    app:layout_constraintTop_toTopOf="@+id/guideline_15"
    app:srcCompat="@drawable/ic_launcher_background" />

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

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

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

    </android.support.constraint.ConstraintLayout>

    输出:

    enter image description here

    关于android-constraintlayout - 如何在指南中将 View 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50244695/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com