【问题标题】:Is this layout possible in Android?这种布局在 Android 中可行吗?
【发布时间】:2020-02-11 15:43:38
【问题描述】:

这种布局看起来很简单,但我找不到任何方法来仅在 xml 中使用LinearLayout 或任何其他布局而不动态更改属性。这是我想要的,如果文本很短,那么布局应该如图所示:

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

【问题讨论】:

  • 我不确定你在找什么......但似乎文本视图的换行内容的布局宽度会做到这一点
  • 如果我们使用LinearLayout,换行内容的简单布局宽度会将按钮推出视图。
  • TextView 中使用android:singleLine=trueandroid:elipesize=end 并将按钮设置为android:toTheRightOf TextView
  • @MuhammadBabar 当文本很短时它不会满足第一个场景。

标签: android android-layout android-linearlayout


【解决方案1】:

在尝试了很多布局之后,我能做的最好的就是使用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>

【讨论】:

【解决方案2】:

设置 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" />

【讨论】:

  • 如果文本很长,这只会将按钮从视图中推出。
  • 不,您可以为按钮提供固定宽度并将其与 parentright=true 对齐
  • 这更糟糕。如果文本很长,它甚至不会将Button 推到视野之外,但如果文本很短,那么Button 会扩展以填充剩余空间。你有没有尝试过?请再次阅读问题并尝试理解我在问什么。
  • 对不起我的错误,请参阅我的更新答案我添加了 maxwidth 属性,它不会让 textview 扩展超过 thiat @M-WaJeEh
  • 这绝对可以解决您的问题,点赞并将其标记为其他人帮助的答案谢谢@M-WaJeEh
【解决方案3】:

试试这个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>

【讨论】:

    【解决方案4】:

    实现此目的的另一种方法是通过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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多