我正在尝试制作 ConstraintLayout 以用相对和线性布局替换常规布局,但在卡片视图内垂直居中两个视图时遇到一些麻烦。
下面的布局文件是我想要替换的当前布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/main_button_side_margin"
android:layout_marginStart="@dimen/main_button_side_margin"
android:layout_marginTop="@dimen/main_button_top_margin"
android:paddingBottom="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="2dp">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/select_language_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/language_stroke"
android:minHeight="80dp">
<ImageView
android:id="@+id/img_language"
android:layout_width="@dimen/main_button_size"
android:layout_height="@dimen/main_button_size"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/main_button_icon_margin"
android:layout_marginStart="@dimen/main_button_icon_margin"
android:src="@drawable/ic_language_white_48dp"
android:tint="@color/language_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/menu_text_margin"
android:layout_marginLeft="@dimen/menu_text_margin"
android:layout_marginRight="@dimen/menu_text_margin"
android:layout_marginStart="@dimen/menu_text_margin"
android:layout_toEndOf="@id/img_language"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/main_button_text_title_margin"
android:text="Text" />
<TextView
android:fontFamily="sec-roboto-light"
android:gravity="start"
android:id="@+id/language_desc"
android:text="description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
我目前的结果如下:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView …Run Code Online (Sandbox Code Playgroud) android android-layout android-constraintlayout constraint-layout-chains
我在约束布局中有两个图像.我想在android studio中使用带有设计页面的链功能,但我找不到代表链功能的图标或菜单.
I'm having an issue to spread-chain 2 groups of elements with Constraint Layout. I understand the goal of this new layout is to use a flat hierarchy so I'd like to avoid putting my elements inside a child layouts.
I looked at some awesome resources like constraintlayout.com but couldn't figure out how to make it work for my specific case - which I think can be common..
这是我想要实现的图像。在红色中,空格1、2和3需要具有相同的高度(就像传播链一样)。
感谢您的关注 :)
我有一个简单的水平链接视图布局,具有 spread_inside 链样式。当我尝试使用偏差属性将视图移动到所需位置时,我发现偏差属性被忽略。
以下是布局供参考:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<View
android:id="@+id/view_1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/black"
app:layout_constraintEnd_toStartOf="@id/view_2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view_2"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="@android:color/black"
app:layout_constraintEnd_toStartOf="@id/view_3"
app:layout_constraintStart_toEndOf="@id/view_1"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view_3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toEndOf="@id/view_2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
在这里,我想使用layout_constraintHorizontal_bias属性将view_3移近view_2。我怎样才能做到这一点?
我想将页面标题居中。标题可能看起来像(1) Dokument1或(3) GreatDocument。我想要左右边距,60dp并且文档名称具有可变长度。当我使用wrap_content居中@+id/title效果很好时,但是边距不适用于长文档名称。当我使用0dp边距时,会受到尊重,但居中不起作用。
如何使 ConstraintLayout 中的打包链居中,同时具有动态长度和边距。
<android.support.constraint.ConstraintLayout
android:id="@+id/top_bar_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/open_brackets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="("
android:gravity="center_vertical"
android:layout_marginStart="60dp"
android:textSize="20sp"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintEnd_toStartOf="@id/page_count"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/page_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:gravity="center_vertical"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@id/open_brackets"
app:layout_constraintEnd_toStartOf="@id/close_brackets"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/close_brackets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=") "
android:gravity="center_vertical"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@id/page_count"
app:layout_constraintEnd_toStartOf="@id/title"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="60dp"
android:text="DocumentTitle"
android:textSize="20sp"
android:gravity="center_vertical"
android:ellipsize="end"
app:layout_constraintStart_toEndOf="@+id/close_brackets"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
(我通过删除对页数和标题的 ViewModel 的引用来简化示例;该文档下面有其他具有较低边距的视图)