【问题标题】:Align TextViews relative to each other相对于彼此对齐 TextView
【发布时间】:2018-03-07 10:51:48
【问题描述】:

我有两个 TextView。 第一个 TextView 包含动态文本,可小可大。 第二个 TextView 包含静态小文本。 第二个 TextView 应该位于第一个 TextView 的右侧。

它应该是这样工作的:

  • 当第一个文本很大时:
  • 当文本很小时:

如何制作这种布局?当第一个 TextView 包含大文本时,使用相对或线性布局 - 第二个文本从屏幕上消失了。

【问题讨论】:

  • 显示你的代码你做了什么 .xml 和 java 文件。

标签: android android-layout textview


【解决方案1】:

试试这个FlexboxLayout

将以下依赖项添加到您的 build.gradle 文件中:

dependencies {
    compile 'com.google.android:flexbox:0.3.2'
}

用法

 <com.google.android.flexbox.FlexboxLayout
        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"
        app:flexWrap="nowrap"
        app:alignItems="stretch"
        app:alignContent="stretch" >

        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ff0"
            android:text="niledfdfdfhfgfdgdfgdfgfgfggdfgdfggggggggggggggggggggggggggggggggggggggggggggggfhssdsdsdsfgdfgfgfgfgfggfgdgdfgfgfgdsddjfhdkjfhjshdf"            />

        <TextView
            android:id="@+id/textview2"
            android:layout_width="wrap_content"
            android:text="nilxcvxceshdfhd"
            android:background="@color/colorAccent"
            android:layout_height="wrap_content"
            app:layout_minWidth="50dp"
            app:layout_minHeight="20dp"
            app:layout_alignSelf="center"

            />

    </com.google.android.flexbox.FlexboxLayout>

输出

大文本输出

【讨论】:

    【解决方案2】:

    你可以添加一个带有方向的LinearLayout:“horizo​​ntal”并在其中添加两个文本视图。

    <LinearLayout
         ...
         android:orientation="horizontal">
       <TextView .../>
       <TextView .../>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      试试这个代码,我认为它会按你的意愿工作

       <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginTop="10dp">
      
      <TextView
          android:id="@+id/txt_item"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="asdasdadasdasdasdasdasdasdasdasdasdasdasdasd"/>
      
      <TextView
          android:layout_weight="1"
          android:layout_width="wrap_content"
          android:text="2asdasdadasdasdasdasdasdasdasdasdasdasdasdasd"
          android:layout_height="wrap_content"/>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 1970-01-01
        • 2012-05-21
        相关资源
        最近更新 更多