【问题标题】:Reusing layouts with ConstraintLayout使用 ConstraintLayout 重用布局
【发布时间】: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


【解决方案1】:

如果您包含布局,所有“膨胀参数”(宽度、高度、边距、填充)都会被忽略。

更改包含文件中的根元素以合并并在其中放置 TextView。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/standard_textview"
        android:layout_width="0dp"
        android:layout_height="@dimen/default_height"
        ... />
</merge>

【讨论】:

  • 这样textview中的params就包含在主布局中了吗?
  • 是的(应该可以使用 AFAIK),我没有将它们全部粘贴,所以它只是显示了根目录的变化。约束布局规则仍然适用(如果包含在其中)。
  • 问题是它没有出现在设计编辑器中。
  • 它出现了,但作为一个“点”,似乎没有包含属性。
  • 问题是:需要在include标签中添加布局约束吗?
猜你喜欢
  • 2017-04-17
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 2017-07-30
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
相关资源
最近更新 更多