android - 具有 ConstraintLayout 和填充的 LinearLayout

标签 android android-layout android-constraintlayout

我正在尝试使用 3 张图像进行布局,它们之间有空间,但我在每张图像下方都有一个 TextView ,我希望 TextView 的宽度比图像大 20dp。

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="top"
       android:weightSum="6"
       android:orientation="horizontal">
       <Space
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="2" />
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:paddingLeft="@dimen/abc_action_bar_content_inset_with_nav"
           android:paddingRight="@dimen/abc_action_bar_content_inset_with_nav"
           android:orientation="vertical">
           <ImageButton
               android:src="@drawable/ic_sad"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/SadImageButton"
               android:background="@null"
               android:scaleType="fitCenter" />
           <android.support.constraint.ConstraintLayout      
           android:layout_width="match_parent"
           android:layout_height="match_parent">
           <TextView
               android:id="@+id/SadTextTextView"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:gravity="center"
               android:visibility="gone"
               app:autoSizeTextType="uniform"
               android:minLines="4"
               android:maxLines="4"
               app:layout_constraintBottom_toBottomOf="parent"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toTopOf="parent"/>
           </android.support.constraint.ConstraintLayout>
       </LinearLayout>
       <Space
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="1" />
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:gravity="center"
           android:orientation="vertical">
           <ImageButton
               android:src="@drawable/ic_neutral"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/NeutralImageButton"
               android:background="@null"
               android:scaleType="fitCenter" />
           <android.support.constraint.ConstraintLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
           <TextView
               android:id="@+id/NeutralTextTextView"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:gravity="center"
               android:visibility="gone"
               android:minLines ="4"
               android:maxLines ="4"
               app:autoSizeTextType="uniform"
               app:layout_constraintLeft_toLeftOf="parent"
               app:layout_constraintRight_toRightOf="parent"/>
           </android.support.constraint.ConstraintLayout>
       </LinearLayout>
       <Space
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="1" />
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:gravity="center"
           android:orientation="vertical">
           <ImageButton
               android:src="@drawable/ic_happy"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/HappyImageButton"
               android:background="@null"
               android:scaleType="fitCenter" />
           <android.support.constraint.ConstraintLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
           <TextView
               android:id="@+id/HappyTextTextView"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:gravity="center"
               android:visibility="gone"
               android:maxLines ="4"
               app:layout_constraintLeft_toLeftOf="parent"
               app:layout_constraintRight_toRightOf="parent"/>
           </android.support.constraint.ConstraintLayout>
       </LinearLayout>
       <Space
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="2" />
   </LinearLayout>

我已经尝试将填充添加到包含 TextView 或 TextView 的图像的布局,但我无法做到我想做的,有什么想法吗?

这是没有文字的图像的外观 Image without text 这就是我在没有约束布局的情况下得到的 image with text 如果我使用约束布局,测试的宽度等于图像,我希望它更大。

最佳答案

我想你想得到这样的东西:

Screenshot

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal">

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="2"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:adjustViewBounds="true"
                android:src="@drawable/icon_red"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="4"
                android:text="@string/lorem_ipsum"
                />

        </LinearLayout>

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:adjustViewBounds="true"
                android:src="@drawable/icon_green"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="4"
                android:text="@string/lorem_ipsum"
                />

        </LinearLayout>

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:adjustViewBounds="true"
                android:src="@drawable/icon_blue"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="4"
                android:text="@string/lorem_ipsum"
                />

        </LinearLayout>

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="2"/>

    </LinearLayout>

</FrameLayout>

关于android - 具有 ConstraintLayout 和填充的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59226840/

相关文章:

android - 如何启用按钮取决于 API 7 中的编辑文本

android - 即使删除了应用程序,Cordova 应用程序中的 Web 存储也会被保留

java - 我如何引用尚未在 xml 文件中创建的 View ?

java - 如何在 Android 开发的 Java 中正确从 ViewPager、 fragment 设置获取当前 'page number'

android - 如何在 editText 空字符上添加占位符

android - 针对不安全的 TrustManager 的 Google Play 安全警报

android-layout - Android - 如何将 SlidingUpPanelLayout 设置为屏幕高度的一半?

android - 应用程序 :layout_constraintBottom_toBottomOf ="parent" for inflated XML layout

android - 如何以编程方式设置 ConstraintLayout 的 XML 属性 'layout_constrainedWidth'?

android - 哪个属性在 Android-Studio 2.2 中替换为 "relativePercent"