我正在尝试使用 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 的图像的布局,但我无法做到我想做的,有什么想法吗?
最佳答案
我想你想得到这样的东西:
这是我的布局:
<?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/