Android动态视图定位

问题描述 投票:0回答:2

如果view2未填满整个宽度,我需要view1view1旁边。如果view1的内容为全角(内容填满屏幕的整个宽度),则view2应该在其下。

例如,如果我的TextView文本短,则在其旁边添加一个时间戳如果TextView文本为多行,则时间戳在其下方

我可以使用ConstraintLayout实现吗?

When view1 is not full width

When view1 is full width

android android-constraintlayout
2个回答
0
投票

是,但是,我建议在头部使用“约束布局”,但使用其他布局(“相对”或“循环”)从视图中组织其他元素。

如果可以确定位置,则可以使用自定义宽度。或者,仍然可以尝试使用“父”,如果将片段分成多个部分,则包裹内容也可以使用。


0
投票

[请尝试使用下面的代码段

<?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:padding="5dp">

    <TextView
        android:id="@+id/text_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_gray_background"
        android:ellipsize="end"
        android:lines="1"
        android:padding="4dp"
        android:text="Text ONE"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@id/textB"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="Text TWO"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

请在下面的链接中检查使用约束布局水平设置的三个textview:Link

如果要使textview相等,则使用belwo代码段:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:id="@+id/txt_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#5d737e"
        android:layout_weight="1"
        android:padding="20dp"
        android:text="ONE" />

    <TextView
        android:id="@+id/txt_two"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:padding="20dp"        
        android:text="TWO" />
</LinearLayout>

[取消此Link

© www.soinside.com 2019 - 2024. All rights reserved.