【发布时间】:2019-05-01 11:24:36
【问题描述】:
我想重用我的基本布局组件,例如按钮和文本视图。文件 main.xml 有一个 ConstraintLayout,当我包含自定义 TextView 的布局时,主布局完全忽略 custom_textview.xml 中固定的 layout_margins。 有一种特殊的方法可以在 ConstraintLayout 中包含/合并布局。
主布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true" >
<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">
<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="0dp"
android:layout_marginStart="@dimen/default_margin"
android:layout_marginTop="@dimen/default_top_bottom_parent_margin"
android:layout_marginEnd="@dimen/default_margin"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round"
tools:srcCompat="@tools:sample/avatars" />
<include
android:id="@+id/name_surname"
layout="@layout/standard_textview_layout"
android:layout_height="56dp"
android:layout_width="match_parent"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
这是自定义TextView的布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/standard_textview"
android:layout_width="0dp"
android:layout_height="@dimen/default_height"
android:textSize="@dimen/default_text_size"
android:layout_margin="@dimen/default_margin"
android:layout_marginBottom="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf=""
app:layout_constraintWidth_max="@dimen/default_max_width"
app:layout_constraintWidth_min="@dimen/default_min_width" />
【问题讨论】:
-
添加你的xml代码
-
我希望你将 constraintLayout 依赖添加到应用级 gradle 文件中
-
是的,我添加了依赖项。
-
问题是TextView布局文件中固定的“layour_margin”在我包含的时候不影响主布局。
标签: android layout constraints android-constraintlayout