Android可以使用这种布局吗?

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

此布局似乎很简单,但是我找不到使用LinearLayout或任何其他布局仅在xml中单独进行此操作的方法,而没有动态更改属性。这就是我想要的,如果文本很短,那么布局应该完全包裹起来,如图所示:

“在此处输入图像描述”

但是如果TextView包含更长的文本,则应将其扩展,但必须为Button留出空间,如下所示:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS82SHRGcS5wbmcifQ==” alt =“在此处输入图像描述”>

android android-layout android-linearlayout
4个回答
3
投票

[尝试了很多布局之后,我能做的最好的就是将TableLayoutshrinkColumns结合使用

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="0" >
    <TableRow>
        <TextView
            android:id="@+id/text"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="This is a very long text and it cannot be displayed wholly" />

        <Button
            android:id="@+id/button"
            android:text="Button" />
    </TableRow>
</TableLayout>

0
投票

使textview的宽度wrapcontent和给按钮toRightOf文本视图然后文本视图将根据文本展开,并且按钮将始终位于文本视图的右侧

        <TextView
        android:id="@+id/tV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="400dp"
        android:text="what ever you want" />

      <Button
        android:id="@+id/btn"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_toRightOf="@id/tV"
        android:layout_alignParentRight="true"
        android:text="Button" />

0
投票

尝试此Wajeeh ........................

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:maxWidth="200dp"
        android:text="kdfjdk" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

0
投票

实现此目的的另一种方法是通过ConstraintLayout,该问题在发布时不可用。请查看此答案https://stackoverflow.com/a/60163899/826606

<androidx.constraintlayout.widget.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="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="April Ludgate April April "
        android:textColor="#000"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/threadType"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap" />

    <TextView
        android:id="@+id/threadType"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:text="SMS"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="@+id/title"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/title"
        app:layout_constraintTop_toTopOf="@+id/title"
        app:layout_constraintWidth_default="wrap" />


</androidx.constraintlayout.widget.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.